Skip to main content
 首页 » 编程设计

c#之WCF 中的多个基地址和多个端点

2024年02月24日24me-sa

我正在使用两个绑定(bind) TCP 和 HTTP。我想为两个绑定(bind)提供 mex 数据。 我想要的是 mexHttpBinding 仅公开 HTTP 服务,而 mexTcpBinding 仅公开 TCP 服务。或者这可能是我仅从 HTTP 绑定(bind)访问 stats 服务而从 TCP 访问 eventLogging 服务?

例如:

  • 对于 TCP,我应该只有

    net.tcp://localhost:9001/ABC/mex 
    net.tcp://localhost:9001/ABC/eventLogging 
    
  • 对于 HTTP

    http://localhost:9002/ABC/stats 
    http://localhost:9002/ABC/mex 
    

当我连接到任何基地址(使用 WCF 测试客户端)时,我是否能够访问所有服务?就像当我连接到 net.tcp://localhost:9001/ABC 时,我能够使用 HTTP 绑定(bind)上提供的服务。为什么会这样?

<system.serviceModel> 
  <services> 
    <service behaviorConfiguration="ABCServiceBehavior" name="ABC.Data.DataServiceWCF"> 
      <endpoint address="eventLogging" binding="netTcpBinding" contract="ABC.Campaign.IEventLoggingService" /> 
      <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange" /> 
      <endpoint address="stats" binding="basicHttpBinding" contract="ABC.Data.IStatsService" /> 
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
      <host> 
        <baseAddresses> 
          <add baseAddress="net.tcp://localhost:9001/ABC" /> 
          <add baseAddress="http://localhost:9002/ABC" /> 
        </baseAddresses> 
      </host> 
    </service> 
  </services> 
  <behaviors> 
    <serviceBehaviors> 
      <behavior name="ABCServiceBehavior"> 
        <serviceMetadata httpGetEnabled="false" /> 
        <serviceDebug includeExceptionDetailInFaults="true" /> 
      </behavior> 
    </serviceBehaviors> 
  </behaviors> 
</system.serviceModel> 

请您参考如下方法:

I want to give mex data on both bindings. What I want is that the mexHttpBinding only exposes the HTTP services while the mexTcpBinding exposes TCP services only. Or is this possible that I access stats service only from HTTP binding and the eventLogging service from TCP?

嗯,在这种情况下,您需要有两个独立的、不同的服务 - 一个只公开 eventLogging,另一个只公开 stats。

当您有两个独立的服务时,您可以通过 HTTP 公开一个,它的 mex 将只显示那些方法,另一个通过 TCP/IP 公开它的方法。

<services> 
  <service name="ABC.Data.DataServiceWCFEventlogging" 
           behaviorConfiguration="ABCServiceBehavior" > 
    <endpoint address="eventLogging"  
              binding="netTcpBinding"  
              contract="ABC.Campaign.IEventLoggingService" /> 
    <endpoint address="mex"  
              binding="mexTcpBinding"  
              contract="IMetadataExchange" /> 
    <host> 
       <baseAddresses> 
         <add baseAddress="net.tcp://localhost:9001/ABC" /> 
       </baseAddresses> 
     </host> 
  </service> 
  <service name="ABC.Data.DataServiceWCFStats" 
           behaviorConfiguration="ABCServiceBehavior" > 
     <endpoint address="stats"  
               binding="basicHttpBinding"  
               contract="ABC.Data.IStatsService" /> 
     <endpoint address="mex"  
               binding="mexHttpBinding"  
               contract="IMetadataExchange" /> 
     <host> 
        <baseAddresses> 
           <add baseAddress="http://localhost:9002/ABC" /> 
        </baseAddresses> 
     </host> 
  </service> 
</services> 

如果您在同一服务上同时使用这两种方法,则无法仅通过 http 公开一部分而通过 tcp/ip 公开另一部分。