Multiple Base Addresses and Multiple Endpoints in WCF

akif picture akif · Mar 8, 2010 · Viewed 8.5k times · Source

I'm using two bindings TCP and HTTP. 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?

For Example:

  • For TCP I should only have

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

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

When I connect with any of the base address (using the WCF Test Client) I'm able to access all the services? Like when I connect with net.tcp://localhost:9001/ABC I'm able to use the services which are offered on the HTTP binding. Why is that so?

<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>

Answer

marc_s picture marc_s · Mar 8, 2010

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?

Well, in this case, you need to have two separate, distinct services - one that exposes eventLogging only, and another one that exposes stats only.

When you have two separate services, you can expose one over HTTP and its mex will only show those methods, and the other over TCP/IP and expose its methods.

<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>

If you have both methods on the same service, there is no way to expose only parts of it over http and another part over tcp/ip.