Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosting multiple WCF services on single TCP port in one windows service

Tags:

wcf

Below is my app.config file snippet of host windows service.

<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 TCP endpoint with port 8001. In windows service I am using below code 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 every ServiceHost object I am calling RegisterServiceHost function,code of this function is as 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();
}

Above code is working correctly without any problem. My question is that all services are sharing same port 8001. How all services able to share same port. Even Net.TCP Port Sharing Service is not enabled on the machine. My other question is that will it cause any performance impact in sharing same port. IF I give unique port like 8001,8002 ,8003 for each service then what are the advantage of it.

like image 436
Sachin Avatar asked Mar 27 '13 15:03

Sachin


1 Answers

They can share the same port because they've all got different paths. Clearly it all works, so the WCF host is intelligent enough to figure out how to get them to share the same listen socket on port 8001. It can then distinguish between requests, because the requests will include the service name which is part of the WCF endpoint configuration.

I wouldn't anticipate this would cause any performance problems, but that does depend entirely on how the WCF service host works.

like image 185
Matthew Walton Avatar answered Nov 17 '22 13:11

Matthew Walton