gpt4 book ai didi

wcf - 使用netTcp绑定(bind)时添加服务引用

转载 作者:行者123 更新时间:2023-12-01 18:57:33 24 4
gpt4 key购买 nike

我有一个 WCF 服务,通过将其接口(interface)复制到示例客户端项目来测试
现在我想通过添加服务引用来正常工作。
该服务托管在 Windows 托管中(使用 installUtil)。
该服务有 2 个项目 - 外部项目(接口(interface) + 数据契约)和内部项目(实现)。
由于某种原因它没有 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.

我看到了here 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/Externals 添加服务引用和 net.tcp://localhost:3040/ExecutionService/Externals/IExecutionService 并且我仍然收到相同的错误。

最佳答案

你需要做:

  1. maxHttpBinding -> mexTcpBinding - 您不能在 net.tcp 端点上使用 mexHttpBinding(它是 mex 而不是 max)
  2. mex 端点的合约必须是 IMetadataExchange - 因为您希望通过此端点提供服务元数据
  3. httpGetEnabled="false",因为没有 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>

为了快速测试配置是否正确,我使用控制台主机应用程序作为服务主机。程序.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 将服务引用添加到您的项目。

关于wcf - 使用netTcp绑定(bind)时添加服务引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15270680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com