使用netTcp绑定时添加服务引用(Add service reference when using

2019-07-21 04:07发布

我有我测试通过复制其接口为样本客户端项目的WCF服务。
现在我想通过添加服务引用正常工作。
该服务在托管(使用Windows托管installUtil )。
该服务具有2个项目 - 的外部(接口+ datacontracts)和内部部件(实现)。
出于某种原因,它没有一个app.config所以我手动添加一个:

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

试图从我的样本客户端添加服务引用导致以下错误:

Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:3040/ExecutionService/Externals.IExecutionService'.
There was no endpoint listening at net.tcp://localhost:3040/ExecutionService/Externals.IExecutionService that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
If the service is defined in the current solution, try building the solution and adding the service reference again.

我看到这里有在app.config中没有必要。
我有点困惑,我与一个初学者WCF
一个很好的如何WPF应用程序可以引用我的服务? 我希望该服务被托管的窗口,我不想跟我拖的DLL。

编辑
我添加元数据终结和我的AppConfig现在看起来是这样的:

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

我试图通过使用添加服务参考net.tcp://localhost:3040/ExecutionServicenet.tcp://localhost:3040/ExecutionService/Externalsnet.tcp://localhost:3040/ExecutionService/Externals/IExecutionService和我仍然得到同样的错误。

Answer 1:

你需要做的:

  1. maxHttpBinding - > mexTcpBinding - 你不能对端点的net.tcp使用mexHttpBinding(和它的MEX不是最大)
  2. 对于MEX终结合同必须IMetadataExchange接口 - 只要你想拥有通过这个端点服务元数据
  3. httpGetEnabled =“假”,因为将没有HTTP端点从中获取元数据
  4. 当我在测试一个简单的控制台主机的解决方案,我需要在<服务>标记Externals.ExecutionService更改名称(当然这取决于你如何实例化服务)

那么你的服务引用将可在: net.tcp://localhost:3040/ExecutionService/mex作为基址net.tcp://localhost:3040/ExecutionService和相对地址MEX端点设置为mex

最终的app.config低于:

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

对于一个快速测试配置是否正确我用的控制台主机的应用程序作为服务宿主。 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();
        }
    }
}

运行控制台应用程序,并尝试通过对服务引用添加到项目中net.tcp://localhost:3040/ExecutionService/mex



Answer 2:

乍一看,你忘了元数据终结



Answer 3:

更改此:

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

至:

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

请注意,这不是maxHttpBinding但mexTcpBinding,你有错字也有。



文章来源: Add service reference when using netTcp binding