目前我有一個可用的 WCF 服務,具有以下 App.Config 端點
<services>
<service behaviorConfiguration="ServiceBehavior" name="ProxyWindowsService.HPCommands">
<endpoint address="" binding="basicHttpBinding" contract="ProxyWindowsService.HPCommandsInterface"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:8004/ProxyService/HPCommands"/>
</baseAddresses>
</host>
</service>
</services>
根據新要求,我們需要在其他埠上打開端點。所以我想要一些像這樣的地址設定,我可以通過某些埠路由某些端點
<baseAddresses>
<add baseAddress="http://127.0.0.1:8004/ProxyService/HPCommands/Command1" />
<add baseAddress="http://127.0.0.1:8005/ProxyService/HPCommands/Command2" />
<add baseAddress="http://127.0.0.1:8006/ProxyService/HPCommands/Command3" />
</baseAddresses>
但是,我無法弄清楚如何修改我的配置和代碼以實作多個系結埠。WCF可以做到這一點嗎?我認為它應該允許我打開多個聽眾
uj5u.com熱心網友回復:
我發現要做的(因為我們沒有使用 IIS,大多數答案似乎傾向于使用 IIS)是在專案中添加多個服務來系結多個 HTTP 偵聽器。配置看起來像這樣:
<services>
<service behaviorConfiguration="ServiceBehavior" name="Service.Commands">
<endpoint address="" binding="basicHttpBinding" contract="Service.CommandsInterface" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:80/Service/Commands" />
</baseAddresses>
</host>
</service>
<service behaviorConfiguration="ServiceBehavior" name="Service.Commands2">
<endpoint address="" binding="basicHttpBinding" contract="Service.CommandsInterface2" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:81/Service/Commands" />
</baseAddresses>
</host>
</service>
<service behaviorConfiguration="ServiceBehavior" name="Service.Commands3">
<endpoint address="" binding="basicHttpBinding" contract="Service.CommandsInterface3" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:82/Service/Commands" />
</baseAddresses>
</host>
</service>
</services>
將所有服務添加到專案安裝程式和主條目如下所示:
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1(), new Service2(), new Service3()
};
ServiceBase.Run(ServicesToRun);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/343751.html
