mexTcpBinding in WCF - IMetadataExchange errors

Dai picture Dai · Apr 5, 2010 · Viewed 17.8k times · Source

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?

Answer

marc_s picture marc_s · Apr 5, 2010

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).