如果有多个带有名称的端点,如何在服务引用中使用WCF服务

时间:2012-07-12 10:34:29

标签: wcf wcf-endpoint

我有非常具体的问题.. 如果我创建一个WCF服务,并且它有多个具有名称的端点,我该如何使用浏览器访问它? 另外如何通过添加服务引用在我的客户端应用程序中访问它?

就像我的配置代码:

<services>
  <service name="MultipleEndpoint.SampleService" behaviorConfiguration="MultipleEndpoint.SampleService">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:55052/SampleService.svc"/>
      </baseAddresses>
    </host>
    <endpoint address="/basic" binding="basicHttpBinding" contract="MultipleEndpoint.ISampleService" bindingConfiguration="basicBinding" >
    </endpoint>
    <endpoint address="/wsHttp" binding="wsHttpBinding" contract="MultipleEndpoint.ISampleService" bindingConfiguration="wsBinding" >          
    </endpoint>
    <endpoint address="/webHttp" binding="webHttpBinding" contract="MultipleEndpoint.ISampleService" behaviorConfiguration="REST">
    </endpoint>        
  </service>
</services>

现在,当我尝试使用

访问它时
http://localhost:55052/SampleService.svc/basic or 
http://localhost:55052/SampleService.svc/wsHttp 

它给我页面/资源找不到IE标准错误信息... 同时我想知道如何在客户端应用程序中添加这种类型的URL作为服务引用?

1 个答案:

答案 0 :(得分:0)

这些服务地址是不同的,并且它们并不是严格要求的,这意味着您无法浏览端点服务,例如 http:// localhost:55052 / SampleService.svc / basic 这些地址用于区分通信中的端点

如果您看到服务的wsdl,则在那里指定这些端点的所有地址。

如果通过“添加服务引用”创建服务的代理,则所有端点都在配置中单独创建,如..

<client>
    <endpoint address="http://localhost:54671/Service1.svc/basic"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
            contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />

    <endpoint address="http://localhost:54671/Service1.svc/ws" binding="wsHttpBinding"
            bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
            name="WSHttpBinding_IService1">
    </endpoint>
    ...
</client>

如果您想使用基本的http端点与服务进行通信,那么您可以通过在ctor中传递相应的端点配置name来为其创建代理。

实施例

// this will uses the basic http endpoint.
Service1Client client = new Service1Client("BasicHttpBinding_IService1");