Add service reference when using netTcp binding

隐身守侯 提交于 2019-11-30 02:08:36

You need to do:

  1. maxHttpBinding -> mexTcpBinding - you cannot use mexHttpBinding on net.tcp endpoint (and it's mex not max)
  2. the contract for mex endpoint must be IMetadataExchange - as you want to have service metadata available through this endpoint
  3. httpGetEnabled="false" as there will be no http endpoint to get metadata from
  4. When I was testing the solution in a simple console host I needed to change name in <service> tag to Externals.ExecutionService (but this depends on how you instantiate the service)

Then your service reference will be available at: net.tcp://localhost:3040/ExecutionService/mex as base address is net.tcp://localhost:3040/ExecutionService and the relative address for the mex endpoint is set to mex

Final app.config is below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
  <service name="Externals.ExecutionService" behaviorConfiguration="Default">
    <endpoint name="TCPEndpoint"
              address=""
              binding ="netTcpBinding"
              contract="Externals.IExecutionService"/>
    <endpoint address="mex"
              binding="mexTcpBinding"
              contract="IMetadataExchange"/>
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:3040/ExecutionService"/>
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="Default">
      <serviceMetadata httpGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

For a quick test if the configuration is correct I used console host app as a service host. Program.cs:

using System;
using System.ServiceModel;

namespace Externals
{
    class Program
    {
        static void Main(string[] args)
        {

            var sh=new ServiceHost(typeof(ExecutionService));
            sh.Open();
            Console.WriteLine("Service running press any key to terminate...");
            Console.ReadKey();
            sh.Close();
        }
    }
}

Run the console app and try to add service reference to your project through net.tcp://localhost:3040/ExecutionService/mex.

At first glance, you've forgotten Metadata Endpoint

Change this:

<endpoint address="mex" 
                  binding="maxHttpBinding" 
                  contract="Externals.IExecutionService"/>

To:

<endpoint address="mex" 
                  binding="mexTcpBinding" 
                  contract="IMetadataExchange"/>

Notice it's not maxHttpBinding but mexTcpBinding, you had typo as well there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!