【问题标题】:mexTcpBinding in WCF - IMetadataExchange errorsWCF 中的 mexTcpBinding - IMetadataExchange 错误
【发布时间】:2011-02-04 11:26:17
【问题描述】:

我想让 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:在服务 Service1 实施的合同列表中找不到合同名称“IMetadataExchange”。将 ServiceMetadataBehavior 添加到配置文件或直接添加到 ServiceHost 以启用对此合同的支持。

我感觉您无法使用 TCP 发送元数据,但这就是为什么有 mexTcpBinding 选项?

【问题讨论】:

    标签: wcf tcp net.tcp


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢!做到了。我不认为 WCF 的配置是当时设计的。 App.config 是一个配置文件,我假设当配置元素被删除时,它只是意味着“我没有明确设置任何配置”而不是“禁用此功能”。更好的方法是:
    • @David:嗯,这是一个可以争论很长时间的选项。 WCF 只是使用“如果它不存在,它就不是活动的”方法。一旦你知道它,就可以了,而且很有意义(你不必把它放在那里并设置 active=false 来禁用它 - 只需将它排除在外)
    猜你喜欢
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    相关资源
    最近更新 更多