1

Our application server exposes 5 WCF services over the net.tcp transport, all on the same port. We've been hosting these during development using WcfSvcHost and I've never had to think about how these manage to use the same port.

We're moving them to a Windows Service now, and now I'm instantiating the ServiceHost instances myself. One of the services uses Streamed TransferMode over Tcp.

When starting these services using a configuration file with WcfSvcHost, they work fine. But in our service it complains about the port being in use.

Should it be possible for the streamed service to use the same port?

Andre Luus
  • 3,692
  • 3
  • 33
  • 46

3 Answers3

2

I solved the problem eventually, after alot of trial and error with programmatic configuration of the bindings.

It seems that something in the binding stack generated when you create a NetTcpBinding allows multiple NetTcpBindings to share a port. The problem was that I needed to make a custom binding.

The Solution ended up being to create a custom binding based on a NetTcpBinding. For example:

        var lBinding = new NetTcpBinding() 
        {
            SendTimeout = TimeSpan.FromMinutes(5),
            ReceiveTimeout = TimeSpan.FromMinutes(5),

            MaxConnections = 100,
            ReliableSession = new OptionalReliableSession 
            { 
                Enabled = true,
                Ordered = true,
                InactivityTimeout = TimeSpan.FromMinutes(30)
            },
            Security = new NetTcpSecurity
            { 
                Mode = SecurityMode.TransportWithMessageCredential,
                Message = new MessageSecurityOverTcp { ClientCredentialType = MessageCredentialType.UserName } 
            },
            MaxReceivedMessageSize = 524288
        };

        var lCustomBinding = new CustomBinding(lBinding);

        // Edit the custom binding elements here

        var lEndpoint = new ServiceEndpoint(lContract, lCustomBinding, new EndpointAddress(pServiceHost.BaseAddresses.First()));
Andre Luus
  • 3,692
  • 3
  • 33
  • 46
  • André, what is it about this custom binding that enables the use of port sharing? – anton.burger Sep 07 '12 at 14:09
  • Hi, sorry for the delayed response. I've changed jobs and wasn't logged in to SO. Point is this: I couldn't get multiple `CustomBinding` endpoints running on the same port, even when they appear to contain the same elements. I had to build the `CustomBinding` by passing it a `NetTcpBinding` in the constructor, and then modifying it. – Andre Luus Sep 14 '12 at 08:29
1

I found another solution to for this issue by using a the RoutingService class. Each contract must still be hosted in it's own ServiceHost, but there can be a RoutingService sitting on top of all of them - and presenting them over an unified "endpoint". I've also written a codeproject article about it. The example code is also available on Bitbucket.

m0sa
  • 10,712
  • 4
  • 44
  • 91
  • Sounds interesting! I'll have to take a look at that at some point. Luckily I've managed to solve the issue I was having. I'll post an answer now to that end. – Andre Luus Mar 01 '12 at 07:26
0

See here about Net.TCP Port Sharing, which is what you're looking for.

You've got to enable the service for that too.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Actually, the other Tcp Services have been sharing a port without that service. Furthermore, how come it works when hosted in WcfSvcHost? – Andre Luus Sep 30 '11 at 11:35