【问题标题】:Sharing the same port for WCF REST and ASP.NET Web API为 WCF REST 和 ASP.NET Web API 共享相同的端口
【发布时间】:2015-03-15 08:04:10
【问题描述】:
我有一个使用 WCF webhttpbinding 实现的现有基于 JSON 的服务。此服务托管在 Windows 服务中。我们也实施了 SSL。现在我计划使用 ASP.NET Web API 创建新的基于 JSON 的服务,该服务应该托管在 Windows 服务中。但问题是客户端位于防火墙后面,它们无法向世界公开许多端口,因此我必须重用已经打开的端口。我知道这是不可能的。 但是有什么解决方法我们可以使用相同的端口来处理 WCF REST 和 ASP.NET Web API 的请求吗?
编辑:我没有兴趣为此创建任何额外的WCF router。
【问题讨论】:
标签:
asp.net-web-api
wcf-rest
webhttpbinding
【解决方案1】:
只要路径不同,WCF REST 和 Web API 就可以共享同一个端口。
例如,
// 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”);
}
WCF 和 Web API 都成功运行并侦听端口 13375。在后台,此端口共享由 HTTP.SYS 模块负责。