-->

Sharing the same port for WCF REST and ASP.NET Web

2019-04-09 20:12发布

问题:

I've an existing JSON based service implemented using WCF webhttpbinding. This service is hosted in Windows service. We've implemented SSL as well. Now I'm planning to create new JSON based services using ASP.NET Web API which should be hosted in windows service. But the problem is the clients are behind firewalls and they cannot expose many ports to world and so I've to reuse the already opened port. I'm aware this is not possible straight. But is there any workaround we could use the same port for handling requests comes to WCF REST and ASP.NET Web API?

EDIT: I'm not interested to create any additional WCF router for that.

回答1:

Both WCF REST and Web API can share the same port as long as the path is different.

For example,

// Starting WCF service in port 13375 (Running in Process 1)
ServiceHost wcfServiceHost = new ServiceHost(typeof(StaffIntegrationService));
wcfServiceHost.addServiceEndPoint(typeof(IStaffIntegrationService), webHttpBinding, “http://localhost:13375/wcf”);
wcfServiceHost.open();


// Start WebAPI in 13375 (Running in Process 2)
using (WebApp.Start<Startup>(url: “http://localhost:13375/api”))
{
   Console.WriteLine(“Service is started”);
}

Both the WCF and Web API ran successfully and listen at port 13375. Under the hood this port sharing is taken care by HTTP.SYS module.