Hosting multiple WCF services on the same TCP port in the same Windows service

The following is a snippet of the Windows host service app.config file.

<services> <service name="Share.Services.MainService"> <endpoint address="net.tcp://localhost:8001/MainService" behaviorConfiguration="netTcpBehavior" binding="netTcpBinding" contract="Share.Services.IClaimService" /> </service> <service name="Share.Services.MainMasterPageService"> <endpoint address="net.tcp://localhost:8001/MainMasterPageService" behaviorConfiguration="netTcpBehavior" binding="netTcpBinding" contract="Share.Services.IMainMasterpageService" /> </service> <service name="Share.Services.SMSService"> <endpoint address="net.tcp://localhost:8001/SMSService" behaviorConfiguration="netTcpBehavior" binding="netTcpBinding" contract="Share.ServiceContracts.Messaging.ISMSService" /> </service> </services> 

There are 3 wcf services configured to use a TCP endpoint with port 8001. On a Windows service, I use the code below to register service hosts

 private static ServiceHost[] Hosts = null; public static void Start() { try { while (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { System.Threading.Thread.Sleep(1000); } BaseLog.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); Hosts = new ServiceHost[] { new ServiceHost(typeof(MainService)), new ServiceHost(typeof(MainMasterPageService)), new ServiceHost(typeof(SMSService)) }; foreach (ServiceHost host in Hosts) { RegisterServiceHost(host); } _log.Info("All Hosts Open"); } catch(Exception e) { _log.Error( "MainServiceHost", e); } } 

For each ServiceHost object, I call the RegisterServiceHost function, the code of this function is below

 public static void RegisterServiceHost(ServiceHost host) { var ops = (from e in host.Description.Endpoints from o in e.Contract.Operations select o).ToList(); ops.ForEach( operation => operation.Behaviors.Add(new MainContextOperationBehavior()) ); host.Open(); } 

The code works without problems. My question is that all services use the same port 8001. How can all services share the same port. Even the Net.TCP Port Sharing Service is not enabled on the computer. My other question is that this will cause any performance impact when sharing the same port. IF I give a unique port, for example 8001,8002, 8003 for each service, then what is its advantage.

+7
source share
1 answer

They can use the same port because they all have different paths. Obviously, everything works, so the WCF host is smart enough to figure out how to get them to share the same socket to listen on port 8001. Then it can distinguish between requests because the requests will include the name of the service that is part of WCF end point.

I would not expect this to cause performance issues, but it depends entirely on how the WCF service host works.

+7
source

All Articles