WCF 中的 mexTcpBinding - IMetadataExchange 错误

发布于 2024-08-28 10:22:26 字数 3325 浏览 4 评论 0原文

我想让 WCF-over-TCP 服务正常工作。我在修改自己的项目时遇到了一些问题,因此我想从 VS2008 中包含的“基本”WCF 模板开始。

这是初始的 WCF App.config,当我运行该服务时,WCF 测试客户端可以很好地使用它:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.web>
        <compilation debug="true" />
    </system.web>
    <system.serviceModel>
        <services>
            <service name="WcfTcpTest.Service1" behaviorConfiguration="WcfTcpTest.Service1Behavior">
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8731/Design_Time_Addresses/WcfTcpTest/Service1/" />
                    </baseAddresses>
                </host>
                <endpoint address="" binding="wsHttpBinding" contract="WcfTcpTest.IService1">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WcfTcpTest.Service1Behavior">
                    <serviceMetadata httpGetEnabled="True"/>
                    <serviceDebug includeExceptionDetailInFaults="True" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

这工作完美,没有任何问题。

我认为将其从 HTTP 更改为 TCP 会很简单:将绑定更改为 TCP 等效项并删除 httpGetEnabled serviceMetadata 元素:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.web>
        <compilation debug="true" />
    </system.web>
    <system.serviceModel>
        <services>
            <service name="WcfTcpTest.Service1" behaviorConfiguration="WcfTcpTest.Service1Behavior">
                <host>
                    <baseAddresses>
                        <add baseAddress="net.tcp://localhost:1337/Service1/" />
                    </baseAddresses>
                </host>
                <endpoint address="" binding="netTcpBinding" contract="WcfTcpTest.IService1">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WcfTcpTest.Service1Behavior">
                    <serviceDebug includeExceptionDetailInFaults="True" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

但是当我运行此命令时,我在 WCF 服务主机中收到此错误:

System.InvalidOperationException:合同名称 'IMetadataExchange ' 在服务 Service1 实现的合同列表中找不到。将 ServiceMetadataBehavior 添加到配置文件或直接添加到 ServiceHost 以启用对此合约的支持。

我感觉你不能使用 TCP 发送元数据,但就是这样,为什么有 mexTcpBinding 选项?

I'm wanting to get a WCF-over-TCP service working. I was having some problems with modifying my own project, so I thought I'd start with the "base" WCF template included in VS2008.

Here is the initial WCF App.config and when I run the service the WCF Test Client can work with it fine:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.web>
        <compilation debug="true" />
    </system.web>
    <system.serviceModel>
        <services>
            <service name="WcfTcpTest.Service1" behaviorConfiguration="WcfTcpTest.Service1Behavior">
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8731/Design_Time_Addresses/WcfTcpTest/Service1/" />
                    </baseAddresses>
                </host>
                <endpoint address="" binding="wsHttpBinding" contract="WcfTcpTest.IService1">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WcfTcpTest.Service1Behavior">
                    <serviceMetadata httpGetEnabled="True"/>
                    <serviceDebug includeExceptionDetailInFaults="True" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

This works perfectly, no issues at all.

I figured changing it from HTTP to TCP would be trivial: change the bindings to their TCP equivalents and remove the httpGetEnabled serviceMetadata element:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.web>
        <compilation debug="true" />
    </system.web>
    <system.serviceModel>
        <services>
            <service name="WcfTcpTest.Service1" behaviorConfiguration="WcfTcpTest.Service1Behavior">
                <host>
                    <baseAddresses>
                        <add baseAddress="net.tcp://localhost:1337/Service1/" />
                    </baseAddresses>
                </host>
                <endpoint address="" binding="netTcpBinding" contract="WcfTcpTest.IService1">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WcfTcpTest.Service1Behavior">
                    <serviceDebug includeExceptionDetailInFaults="True" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

But when I run this I get this error in the WCF Service Host:

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

I get the feeling that you can't send metadata using TCP, but that's the case why is there a mexTcpBinding option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2024-09-04 10:22:26

好吧,如果您想要元数据 - TCP 或 HTTP - 您仍然需要包含 serviceMetadata 行为!

<behaviors>
    <serviceBehaviors>
        <behavior name="WcfTcpTest.Service1Behavior">
            <serviceDebug includeExceptionDetailInFaults="True" />
            <serviceMetadata />
        </behavior>
    </serviceBehaviors>
</behaviors>

当然,您不能在其上添加“HttpGetEnabled” - 但行为本身必须存在才能启用元数据交换(以及 IMetadataExchange 合约)。

Well, if you want to have metadata - TCP or HTTP - you still need to include the serviceMetadata behavior!

<behaviors>
    <serviceBehaviors>
        <behavior name="WcfTcpTest.Service1Behavior">
            <serviceDebug includeExceptionDetailInFaults="True" />
            <serviceMetadata />
        </behavior>
    </serviceBehaviors>
</behaviors>

Sure, you can't have a "HttpGetEnabled" on it - but the behavior itself must be present in order to enable exchange of metadata (and thus the IMetadataExchange contract).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文