WCF服务在2个不同的服务合同上公开2个端点

时间:2012-12-06 10:53:59

标签: c# wcf web-services endpoint servicecontract

我正在尝试配置一个WCF服务,以便在不同的URL下公开2个端点,引用不同的功能。

我想要的是 Service1 ,暴露方法A,B,C和 Service2 ,暴露方法D,E。 我希望能够同时浏览 localhost / WebServiceName / Service1 / Service.svc localhost / WebServiceName / Service2 / Service.svc

引用 localhost / WebServiceName / Service1 / Service.svc 的其他应用程序应该只看到包含方法A,B和C的界面。他们不应该看到有关 Service2 的任何内容接口。对于 Service2 同样。

到目前为止,我已在我的WCF服务中定义了两个接口, I_Service1 I_Service2

我在web.config中添加了两个端点,如下所示:

<endpoint address="http://localhost/WebServiceName/Service1/" binding="wsHttpBinding" contract="WebServiceName.I_Service1" bindingConfiguration="Binding1" />
<endpoint address="http://localhost/WebServiceName/Service2/" binding="wsHttpBinding" contract="WebServiceName.I_Service2" bindingConfiguration="Binding2" />  

在此处使用完整地址的建议来自:Multiple endpoints under IIS

但是,我仍然无法浏览 localhost / WebServiceName / Service1 / Service.svc 。我收到了:

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

我可以成功浏览 localhost / WebServiceName / Service.svc ,而wsdl包含方法A,B,C,D,E。但这在我想要的行为中应该是错误的。

有没有我错过的东西?

更新:在本文http://allen-conway-dotnet.blogspot.ro/2011/09/exposing-multiple-binding-types-for.html之后,我为这些端点创建了两种不同的合同服务。但是目前我只浏览 Service1 Service2 显然不存在(出现HTTP 404错误相关问题)。

配置如下:

<services>
   <service behaviorConfiguration="WebServiceName.ServiceBehavior1" name="WebServiceName.Service1">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1"
     contract="WebServiceName.I_Service1" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
     <host>
       <baseAddresses>
         <add baseAddress="http://localhost/WebServiceName/Service1/Service.svc" />
       </baseAddresses>
     </host>
   </service>
   <service behaviorConfiguration="WebServiceName.ServiceBehavior2" name="WebServiceName.Service2">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1"
     contract="WebServiceName.I_Service2" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
     <host>
       <baseAddresses>
         <add baseAddress="http://localhost/WebServiceName/Service2/Service.svc" />
       </baseAddresses>
     </host>
   </service>
  </services>

3 个答案:

答案 0 :(得分:3)

我一直这样做的方式如下:为服务设置一个“基地址”,并将端点地址指定为附加到该基地址的不同文本......

 <service name="MyNamespace.MyService">
    <endpoint address="FirstEndpointAddress" binding="netTcpBinding"
      name="FirstEndpointName"
      contract="MyNamespace.FirstEndpointContract" />
    <endpoint address="SecondEndpointAddress" binding="netTcpBinding"
      name="SecondEndpointName"
      contract="MyNamespace.SecondEndpointContract" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8733/MyBaseAddress" />
      </baseAddresses>
    </host>
  </service>

因此,在您的情况下,基址可能是localhost/WebServiceName,端点1的端点地址可能是Service1/Service.svc。同样,对于端点2,它可能是Service2/Service.svc。我可以看到你已经按照建议将完整地址放在端点地址中,但我能说的就是我已经成功地这样做了。

答案 1 :(得分:2)

目前我解决这个问题的方法是在我的webservice中加入两个.svc文件来分隔这两个接口。 这样,我有 localhost / WebServiceName / Service1.svc localhost / WebServiceName / Service2.svc

使用端点配置

<services>
   <service behaviorConfiguration="WebServiceName.ServiceBehavior1" name="WebServiceName.Service1">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1"
     contract="WebServiceName.I_Service1" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
     <host>
       <baseAddresses>
         <add baseAddress="http://localhost/WebServiceName/Service1.svc" />
       </baseAddresses>
     </host>
   </service>
   <service behaviorConfiguration="WebServiceName.ServiceBehavior2" name="WebServiceName.Service2">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding2"
     contract="WebServiceName.I_Service2" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
     <host>
       <baseAddresses>
         <add baseAddress="http://localhost/WebServiceName/Service1.svc" />
       </baseAddresses>
     </host>
   </service>
  </services>

这个解决方案不是最好的解决方案(如果客户真的想要,它可以发现这个服务公开了2个不同的接口,但我可以使用不同的凭证/令牌来保护它们)。但此刻我会继续使用它。

答案 2 :(得分:1)

我试着制作你描述的东西。所有这些都成功了。如果有一些步骤对你来说很明显,请不要生气。所以:

  1. 创建WCF服务应用程序项目。
  2. 添加两个WCF服务项(默认情况下将是create interface和svc file)。
  3. 创建Web应用程序项目。
  4. 使用“发现”按钮添加两个服务引用(右键单击Web App项目==&gt;添加服务引用)(应该可以看到两个Web服务)。
  5. 就是这样。
  6. 默认情况下,我在Web应用程序项目中获得了以下Web.config:

         <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
        <binding name="BasicHttpBinding_IService2" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:3597/Service1.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
        name="BasicHttpBinding_IService1" />
      <endpoint address="http://localhost:3597/Service2.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IService2" contract="ServiceReference2.IService2"
        name="BasicHttpBinding_IService2" />
    </client>
    

    您可以尝试重建WCF项目和更新WebReferences。