使用netTcp绑定时添加服务引用 [英] Add service reference when using netTcp binding

查看:26
本文介绍了使用netTcp绑定时添加服务引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务,我通过将其接口复制到示例客户端项目来测试.
现在我想通过添加服务引用来正常工作.
该服务托管在 Windows 主机中(使用 installUtil).
该服务有 2 个项目 - 外部(接口 + 数据合同)和内部(实现).
出于某种原因,它没有 app.config,所以我手动添加了一个:

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.
The service is hosted in windows hosting (using installUtil).
The service has 2 projects - externals (interfaces + datacontracts) and internals (implementations).
For some reason it didn't have an app.config so I added one manually:

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

尝试从我的示例客户端添加服务引用会导致以下错误:

Trying to add a service reference from my sample client causes the following error:

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.

I saw here that there's no need in app.config.
I'm a bit confused and I'm a beginner with WCF.
How can a nice WPF app reference my service? I want the service to be windows hosted and I don't want to drag dlls with me.

编辑
我添加了一个元数据端点,我的 appconfig 现在看起来像这样:

Edit
I added a metadata endpoint and my appconfig now looks like this:

<?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/Externals 添加服务引用和 net.tcp://localhost:3040/ExecutionService/Externals/IExecutionService 我仍然遇到同样的错误.

I tried adding a service reference by using net.tcp://localhost:3040/ExecutionService, net.tcp://localhost:3040/ExecutionService/Externals and net.tcp://localhost:3040/ExecutionService/Externals/IExecutionService and I'm still getting the same error.

推荐答案

你需要做的:

  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

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

最终的 app.config 如下:

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>

为了快速测试配置是否正确,我使用控制台主机应用程序作为服务主机.程序.cs:

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();
        }
    }
}

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

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

这篇关于使用netTcp绑定时添加服务引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆