繁体   English   中英

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

[英]WCF service exposing 2 endpoints on 2 different service contracts

我有一个WCF服务,我正在尝试对其进行配置,以便它在不同的URL下公开2个终结点,引用不同的功能。

我要拥有的是Service1 ,公开方法A,B,C和Service2 ,公开方法D,E。我希望能够浏览localhost / WebServiceName / Service1 / Service.svclocalhost / WebServiceName / Service2 / Service .svc

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

到目前为止,我已经在WCF服务中定义了两个接口I_Service1I_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" />  

建议在此处使用完整地址: 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/exposed-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>

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

 <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 我可以看到您已经按照建议将完整地址放入端点地址,但是我只能说我已经成功地做到了这一点。

目前,我对这个问题的解决方案是在我的Web服务中合并两个.svc文件以分隔两个接口。 这样,我有localhost / WebServiceName / Service1.svclocalhost / 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个不同的接口,但是我可以使用不同的凭据/令牌来保护它们)。 但是此刻,我将继续。

我试图做你所描述的事情。 所有这些都成功了。 如果某些步骤对您很明显,请不要生气。 所以:

  1. 创建WCF服务应用程序项目。
  2. 添加两个WCF服务项(默认情况下将是create interface和svc文件)。
  3. 创建Web应用程序项目。
  4. 使用“发现”按钮添加两个服务引用(在Web App Project上右键单击==>“添加服务引用”)(两个Web服务应该可见)。
  5. 就这样。

默认情况下,我在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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM