Search code examples
c#asp.netwcfnettcpbindingself-hosting

Self Hosting netTcpBinding Service in Application_Start


Heres the background -- I want to self-host a WCF service implementing netTcpBinding and it has to be compatible with Windows Server 2003 / IIS6.

I am trying to see if its possible to self-host during the Application_Start event in global.asax:

protected void Application_Start(object sender, EventArgs e)
{
        NetTcpBinding binding = new NetTcpBinding();

        binding.PortSharingEnabled = true;

        ServiceHost host = new ServiceHost(typeof(Svc), new Uri("net.tcp://xxx.x.xxx.xx:1100"));

        string address = "Tcp";

        host.AddServiceEndpoint(typeof(IService), binding, address);

        host.Open(); 
}

But I am receiving this on the client when trying to call the service:

Could not connect to net.tcp://xxx.x.xxx.xx:1100/Tcp. The connection attempt lasted for a time span of 00:00:01.0630608. TCP error code 10061: No connection could be made because the target machine actively refused it xxx.x.xxx.xx:1100.

My first instinct was to see if this was a port issue, so I opened the relevant port on the client but it didnt help, could the server port matter? I can access the port otherwise.

Or if this is occuring because I am using IIS6, what sort of work-around using self-hosting can I use to make this work with IIS6? Does it have to a self-host via console app / windows service or can I self host within global.asax as seen above?

Thanks.


Solution

  • There is nothing easier. Use windows service. Application in IIS is unloaded when IIS wants to so you never know if your service runs or not and there can be other problems as well.

    Windows 2003 + Net.Tcp = Windows service. Anything else is wrong.