gpt4 book ai didi

c# - 公开 net.tcp 端点

转载 作者:太空狗 更新时间:2023-10-29 17:39:27 25 4
gpt4 key购买 nike

我对如何在 WCF 中公开端点感到有点困惑

我有一个 tcp 端点和一个 mex tcp 端点。

<service name="MessageReaderService.MessageReaderService">
<endpoint name="NetTcpReaderService"
address="ReaderService"
binding="netTcpBinding" bindingConfiguration=""
contract="Contracts.IMessageReaderService" />
<endpoint name="netTcpMex"
address="mex"
binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8082" />
</baseAddresses>
</host>
</service>

当我尝试在服务主机中运行它时,出现以下异常:

The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service MessageReaderService. Add a ServiceMetadataBehavior to the
configuration file or to the ServiceHost directly to enable support for this contract.

所以我从这个错误中得出结论,我需要添加一个服务行为来公开元数据。

所以我添加了行为:

<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>

但是我得到了一个不同的错误:

The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the HttpGetUrl property is a relative address, but there is no http base address. Either supply an http base address or set HttpGetUrl to an absolute address.

  1. 所以现在我必须实际添加另一个端点 (http) 以通过 mexhttpbinding 公开元数据?
  2. 是否有通过 tcp 公开端点的简单方法?

最佳答案

两件事:

(1) 一旦定义了服务行为,您当然还必须将其应用到服务中!

<service name="MessageReaderService.MessageReaderService"
behaviorConfiguration="ServiceBehavior">

(2) 您不需要 HTTP 端点 - 您不需要 HTTP URL - 只需像这样定义此服务行为:

<behavior name="ServiceBehavior">
<serviceMetadata />
</behavior>

您的元数据现在可通过 mexTcpBinding 端点访问 - 您无法使用 HTTP 浏览到它,但客户端绝对可以连接到它并使用它!

您可以使用 WCF Test Client 来验证这一点并前往

net.tcp://localhost:8082        (the base address)

net.tcp://localhost:8082/mex    (the mex address)

在这两种情况下,WCF 测试客户端现在应该可以找到您的服务并能够发现其功能。

关于c# - 公开 net.tcp 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10145086/

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