C# – exposing net.tcp endpoint

cwcf

I'm a bit confused of how to expose an endpoint in WCF

I've got a tcp endpoint and a mex tcp endpoint.

<service name="MessageReaderService.MessageReaderService">
    <endpoint name="NetTcpReaderService" 
        address="ReaderService" 
        binding="netTcpBinding" bindingConfiguration=""
        contract="Contracts.IMessageReaderService" />
    <endpoint name="netTcpMex" 
        address="mex" 
        binding="mexTcpBinding" bindingConfiguration=""
        contract="IMetadataExchange" />
    <host>
       <baseAddresses>
           <add baseAddress="net.tcp://localhost:8082" />
       </baseAddresses>
    </host>
 </service>

When I try to run this in the service host I get the following exception :

The contract name 'IMetadataExchange' could not be found in the list of contracts
implemented by the service MessageReaderService. Add a ServiceMetadataBehavior to the
configuration file or to the ServiceHost directly to enable support for this contract.

So I conclude from this error that I need to add a service behavior to expose metadata.

So I added the behavior :

<behavior name="ServiceBehavior">
    <serviceMetadata httpGetEnabled="true"/>                  
</behavior> 

but then I get a different error :

The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the
HttpGetUrl property is a relative address, but there is no http base address. Either
supply an http base address or set HttpGetUrl to an absolute address.

  1. So now I have to actually add another endpoint (http) to expose the metadata over mexhttpbinding ?
  2. is there a simple way to expose the endpoint over tcp ?

Best Answer

Two things:

(1) once you've defined the service behavior, you of course must also apply it to the service!

<service name="MessageReaderService.MessageReaderService"
         behaviorConfiguration="ServiceBehavior">

(2) you don't need an HTTP endpoint - you don't need to have an HTTP URL - just define this service behavior like this:

<behavior name="ServiceBehavior">
    <serviceMetadata />
</behavior> 

Your metadata is now available over a mexTcpBinding endpoint - you cannot browse to it using HTTP, but a client can definitely connect to it and use it!

You can verify this by using the WCF Test Client and going to either

net.tcp://localhost:8082        (the base address)

or

net.tcp://localhost:8082/mex    (the mex address)

in both cases, the WCF Test Client should now find your service and be able to discover its capabilities.

Related Topic