簡體   English   中英

WCF服務偵聽多個端口

[英]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>

我要注意

  1. 該問題的當前“解決方案”是更改安裝程序,以允許我們在服務器上兩次安裝服務。
  2. 該服務的目的是處理多個並發請求,這些並發請求部分地傳輸大數據(如果這是相關的)。

有人可以提供一個如何完成此操作的示例嗎?

另外,我不了解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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM