[英]WCF Service listening to multiple ports
我需要在單個服務器上安裝WCF服務,但是我需要偵聽多個端口(由於運行客戶端的計算機存在各種問題)。 我不清楚這是否可以使用一個ServiceHost
和:一個具有兩個endpoints
“類源自”(CDF) ServiceBase
來完成,或者是否需要兩個CDF ServiceBase
以及一個endpoint
。 以下是相關文件中的相關代碼:
Program.cs中
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
Service1.cs
public partial class Service1 : ServiceBase
{
private wcfServer _server;
ServiceHost serviceHost;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(String[] args)
{
_server = new wcfServer();
serviceHost = new ServiceHost(_server);
serviceHost.Open();
}
protected override void OnStop()
{
if (serviceHost != null)
serviceHost.Close();
}
}
wcfServer.cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode=ConcurrencyMode.Multiple)]
public class wcfServer : IwcfServer { /* ... */ }
的app.config
<services>
<service name="MyService.wcfServer">
<endpoint address=""
bindingConfiguration="BigDataBinding"
binding="basicHttpBinding"
contract="DataBackups.IwcfServer">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/DataBackups/wcfServer/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
我要注意
有人可以提供一個如何完成此操作的示例嗎?
另外,我不了解app.config
<service>
配置如何與以編程方式創建的CDF ServiceBase
實例相關。 在當前設置中, app.config
只有一個Service1
實例和一個配置,因此我想它們會“排隊”。 但是,如果我有多個Service1
實例(可能需要)怎么辦? 如何“選擇”正在配置的服務?
您的WCF
服務在多個端口上偵聽應該沒有問題。 您只需baseAddress
如下所示擴展app.config
文件的baseAddress
部分:
<baseAddresses>
<add baseAddress="http://localhost:80/DataBackups/wcfServer/" />
<add baseAddress="http://localhost:8732/DataBackups/wcfServer/" />
</baseAddresses>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.