简体   繁体   English

WCF服务侦听多个端口

[英]WCF Service listening to multiple ports

I need to install a WCF service on a single server but I need to listen to multiple ports (due to various issues with machines running the client). 我需要在单个服务器上安装WCF服务,但是我需要侦听多个端口(由于运行客户端的计算机存在各种问题)。 It's not clear to me if this can be done with one ServiceHost and: one "class dervied from"(CDF) ServiceBase with two endpoints , or if I need two CDF ServiceBase s each with a single endpoint . 我不清楚这是否可以使用一个ServiceHost和:一个具有两个endpoints “类源自”(CDF) ServiceBase来完成,或者是否需要两个CDF ServiceBase以及一个endpoint Here is the relevant code in relevant files: 以下是相关文件中的相关代码:

Program.cs Program.cs中

static class Program
{
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
        {
            new Service1()
        };
        ServiceBase.Run(ServicesToRun);
    }
}

Service1.cs 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 wcfServer.cs

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
    ConcurrencyMode=ConcurrencyMode.Multiple)]
public class wcfServer : IwcfServer { /* ... */ }

app.config 的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>

I should note that 我要注意

  1. the current "solution" to this issue is to alter the installer to allow us to install the service on the server twice . 该问题的当前“解决方案”是更改安装程序,以允许我们在服务器上两次安装服务。
  2. the purpose of the service is to handle multiple concurrent requests that, in part, stream big data (in case this is relevant). 该服务的目的是处理多个并发请求,这些并发请求部分地传输大数据(如果这是相关的)。

Can someone provide an example of how to accomplish this? 有人可以提供一个如何完成此操作的示例吗?

Also, I do not understand how <service> configurations in app.config relate to CDF ServiceBase instances created programmatically. 另外,我不了解app.config <service>配置如何与以编程方式创建的CDF ServiceBase实例相关。 In the current setup, there is only a single Service1 instance and a single configuration in app.config , so I imagine they "line up." 在当前设置中, app.config只有一个Service1实例和一个配置,因此我想它们会“排队”。 But, what if I there are multiple Service1 instances (which I might need)? 但是,如果我有多个Service1实例(可能需要)怎么办? How do I "choose" which service I am configuring? 如何“选择”正在配置的服务?

Your WCF Service should have no issue listening on multiple ports. 您的WCF服务在多个端口上侦听应该没有问题。 You just need to expand the baseAddress section of your app.config file as follows: 您只需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