WCF中的mexTcpBinding - IMetadataExchange错误

时间:2010-04-05 15:43:37

标签: wcf tcp net.tcp

我想让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选项呢?

1 个答案:

答案 0 :(得分:20)

好吧,如果你想拥有元数据 - TCP或HTTP - 你还需要包含serviceMetadata行为!

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

当然,你不能拥有“HttpGetEnabled” - 但必须存在行为本身以便能够交换元数据(以及IMetadataExchange合同)。