如何在同一台机器上使用不同的主机名托管 2 个 WCF 自托管服务(使用主机 header)

How can I host 2 WCF self hosted services using different host names on the same machine (using the host header)

我正在尝试自行托管两个具有以下url约定的WCF服务:

https://service.servicehost.com/service

https://service-branch.servicehost.com/service

我得到这个错误:

Service cannot be started. System.ServiceModel.AddressAlreadyInUseException: HTTP could not register URL https://+:443/service/. Another application has already registered this URL with HTTP.SYS. --->

我理解这个错误并且我知道如何通过更改服务的后缀来解决这个问题,但似乎 WCF 自主机不支持像 IIS 那样的主机 header 功能(因为我看到+ 号)。

有什么办法可以实现吗?

我不确定 SelfHost/Http 是否可以这样做。使用 Net.Tcp,您将不得不使用 PortSharing 来实现这一点。

最坏的情况是您可能会创建一个 Routing Service 将您的请求路由到正确的服务,其中每个服务都在不同的端口上侦听。

此外,请查看 HostNameComparisonMode Enumeration. The docs for basicHttpBinding 的文档,了解如何使用它。