Add service reference when using netTcp binding

后端 未结 3 960
忘了有多久
忘了有多久 2020-12-28 09:37

I have a WCF service that I tested by copying its interfaces to a sample client project.
Now I want to work properly by adding a service reference.

相关标签:
3条回答
  • 2020-12-28 10:23

    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.

    0 讨论(0)
  • 2020-12-28 10:32

    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.

    0 讨论(0)
  • 2020-12-28 10:42

    At first glance, you've forgotten Metadata Endpoint

    0 讨论(0)
提交回复
热议问题