使用netTcp绑定时添加服务引用

14

我有一个WCF服务,我通过将其接口复制到示例客户端项目中进行了测试
现在我想通过添加服务引用来使其正常工作。
该服务托管在Windows主机上(使用installUtil)。
该服务有两个项目——外部(接口+数据契约)和内部(实现)。
由于某种原因,它没有一个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应用程序引用我的服务?我希望该服务是基于Windows托管的,并且不想拖着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 添加服务引用,但仍然出现相同的错误。

3个回答

32
您需要进行以下操作:
  1. 将maxHttpBinding更改为mexTcpBinding-在net.tcp端点上不能使用mexHttpBinding(且应该是mex而不是max)
  2. 针对mex端点,合同必须是IMetadataExchange-因为您想要通过此端点提供服务元数据
  3. 将httpGetEnabled="false" -因为没有用于获取元数据的http端点
  4. 当我在简单的控制台主机上测试解决方案时,我需要在<service>标记中将名称更改为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 为您的项目添加服务引用。

2

嗨,我添加了一个,但仍然出现相同的异常,请查看帖子中的编辑。 - Noich

2

改成这样:

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

To:

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

注意这里不是maxHttpBinding而是mexTcpBinding,你打错了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接