具有 5 个以上的 WCF 项目。其中 4 个必须作为 Windows 服务托管,1 个必须托管在 IIS 中。全部在同一台机器上。
对于 4 个 WCF 项目中的每一个,我都需要单独承载 4 个 Windows 服务项目。为了尽量减少要维护的项目数量,我正在考虑使用一个 Windows 服务来安装所有 4 个 WCF 项目,以便于维护。无论如何,除了OnStart和OnStop之外,我调用wcf,没有其他逻辑。
我看到的挑战是,每个 Windows 服务都需要与 WCF 项目中使用的相同应用配置文件。如果我通过从应用程序设置中获取服务名称来动态执行此操作,我将如何在运行时加载不同 wcf 项目的 app.config 文件以作为 Windows 服务托管。
这可行吗?如果是这样,我该如何实现?
是的,这是可能的,您只需要在配置文件中配置每个端点。如何做到这一点:
-
Windows Service WCF hoster 是 ServiceHost 类,因此您需要为每个协定创建 4 个主机。
var serviceHost = new ServiceHost(typeof(CommunicationManagement((; serviceHost.Open((
-
现在,您可以在"服务"部分中配置每个服务终结点:
<system.serviceModel>
<services>
<service name="Communication.Service.CommunicationManagement">
<endpoint
binding="netTcpBinding"
bindingConfiguration="myBinding"
contract="Communication.Service.ICommunicationManagement"
name="CommunicationManagement">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="MEX" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Communication/Service/CommunicationManagement" />
</baseAddresses>
</host>
</service>
<service bname="Communication.Service.Managers.PhoneAdatpersManager">
<endpoint
binding="netTcpBinding"
bindingConfiguration="myBinding"
contract="Communication.IPhoneAdministration"
name="PhoneAdministration">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="MEX" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Communication/Service/PhoneAdministration" />
</baseAddresses>
</host>
</service>
</services>
<system.serviceModel>
- 上面你可以看到
PhoneAdatpersManager
和CommunicationManagement
服务,它们托管在单个Windows服务中,并在8000端口上协同工作(但您可以使用不同的端口(。