tags:

views:

727

answers:

1

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?

+3  A: 

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

marc_s
Thank you! That did it.I don't think WCF's configuration was designed right then. App.config is a configuration file, I assumed that when a configuration element was removed it simply means "I don't explicitly set any configuration" not "Disable this feature". A better way would be this:<serviceMetadata enabled="true" />
David
@David: well, that's an option one could argue about for a long time. WCF just uses the "if it's not there, it's not active" approach. Once you know it, it's okay and makes a lot of sense (you don't have to put it in there and set active=false to disable it - just leave it out)
marc_s