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

僤鯓⒐⒋嵵緔 提交于 2019-12-05 01:48:54

问题


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.



来源:https://stackoverflow.com/questions/27988675/sharing-the-same-port-for-wcf-rest-and-asp-net-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!