Search code examples
wcfnettcpbindingself-hosting

Problems with netTCP Binding and Self hosting


I have searched the web but I cannot find the solution to my problem.

I am self hosting my service on XP using nettcpbinding. The config file is as follows:

<system.serviceModel>

    <diagnostics>
      <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
        logMessagesAtTransportLevel="true" />
    </diagnostics>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />

    <services>       
 <service behaviorConfiguration="behaviorconfig"
        name="myservice">
         <host>
          <baseAddresses>
            <add baseAddress="net.tcp://10.1.3.186:8001/myService" />
          </baseAddresses>
        </host>
        <endpoint address=""
               binding="netTcpBinding"
               bindingConfiguration="BindingConfiguration"
               contract="xxx.ISomeService" />

        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />       
      </service>
 </services>
    <bindings>
           <netTcpBinding>
        <binding name="BindingConfiguration" receiveTimeout="10:00:00"
          sendTimeout="10:00:00" maxBufferSize="65536" maxReceivedMessageSize="65536"
          transferMode="Streamed">
          <readerQuotas maxDepth="65536" maxStringContentLength="65536"
            maxArrayLength="65536" maxBytesPerRead="97108864" maxNameTableCharCount="65536" />
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviorconfig">
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>

      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

AND the selfhost code is

Uri tcpUrl = new Uri("net.tcp://10.1.3.186:8001/myService");
            //Create ServiceHost
            ServiceHost host
            = new ServiceHost(typeof(xxx.SomeService), tcpUrl);
            //Add a service endpoint
            host.AddServiceEndpoint(typeof(xxx.ISomeService)
            , new NetTcpBinding(), "");
            //Enable metadata exchange

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = false;
            host.Description.Behaviors.Add(smb);
            //Start the Service
            host.Open();

When I try to add a service reference I get the error Error: Cannot obtain Metadata from net.tcp://10.1.3.186:8001/myService/mex

What is wrong here?


Solution

  • Looks like you're missing the mex endpoint:

    host.AddServiceEndpoint(typeof(IMetadataExchange),MetadataExchangeBindings.CreateMexTcpBinding(), "mex");

    Slight revision to clarify: The mex endpoint is required so that information about what is available on the service can be exchanged. You add this in addition to your main TCP listener. It's automatically added when using http based services (wsdl, etc)