8

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

我想要的是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" />  

在 enpoint 中使用完整地址的建议来自这里: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>
4

3 回答 3

4

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

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

于 2012-12-10T13:42:59.587 回答
3

目前我对这个问题的解决方案是在我的 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 个不同的接口,但我可以用不同的凭据/令牌保护它们)。但目前我会接受它。

于 2012-12-21T14:22:00.483 回答
1

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

  1. 创建 WCF 服务应用程序项目。
  2. 添加两个 WCF 服务项(默认为创建接口和 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。

于 2012-12-06T11:17:24.247 回答