C# – Url registration issue with WCF when Http binding is used

cnetwcfwcf-binding

I'm trying to host two WCF services in one application. I want them to share the same BaseAddress but have their own URLs something like: net.tcp://localhost:1234/service1 and net.tcp://localhost:1234/service2

The following config allows me to do that:

<system.serviceModel>
    <services>
        <service name="VanillaWcf.Shared.MyService" behaviorConfiguration="beh">
            <endpoint address="myservice" binding="netTcpBinding" name="tcpEndPoint" contract="VanillaWcf.Shared.IMyService" />
            <endpoint address="myservice" binding="wsHttpBinding" name="httpEndPoint" contract="VanillaWcf.Shared.IMyService"/>
            <host>
                <baseAddresses>
                    <add baseAddress="net.tcp://localhost:1234" />
                </baseAddresses>
            </host>
        </service>
        <service name="VanillaWcf.Shared.SecondService" behaviorConfiguration="beh">
            <endpoint address="secondService" binding="netTcpBinding" name="tcpEndPoint" contract="VanillaWcf.Shared.ISecondService"/>
            <endpoint address="secondService" binding="wsHttpBinding" name="httpEndPoint" contract="VanillaWcf.Shared.ISecondService"/>
            <host>
                <baseAddresses>
                    <add baseAddress="net.tcp://localhost:1234"/>
                </baseAddresses>
            </host>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="beh">
                <serviceMetadata httpGetEnabled="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

And the code is:

        ServiceHost host = new ServiceHost(typeof(MyService));
        ServiceHost secondHost = new ServiceHost(typeof(SecondService));

        host.Open();
        secondHost.Open();

This works fine.

However, I get an exception when I add http://localhost:4321 as the base address of both services in the config.

The exception is:
The ChannelDispatcher at 'http://localhost:4321/' with contract(s) '"IHttpGetHelpPageAndMetadataContract"' is unable to open its IChannelListene'
with an inner exception of
A registration already exists for URI 'http://localhost:4321

I don't have any MEX configuration and I don't want it.

Note: My NetPortSharing service is disabled.

Best Answer

You can't have two base address with the same scheme (net.tcp) and the same address - you'll have to change something.

One possibility might be to not use base addresses, but completely specify the net.tcp addresses on your two endpoints. This might work (should, because the whole address is different for the two services).

<service name="VanillaWcf.Shared.MyService" behaviorConfiguration="beh">
   <endpoint name="tcpEndPoint" 
       address="net.tcp://localhost:1234/myservice1" 
       binding="netTcpBinding" 
       contract="VanillaWcf.Shared.IMyService" />
</service>

Marc

Related Topic