在一个 Windows 服务中的单个 TCP 端口上托管多个 WCF 服务 [英] Hosting multiple WCF services on single TCP port in one windows service

查看:41
本文介绍了在一个 Windows 服务中的单个 TCP 端口上托管多个 WCF 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的主机 Windows 服务的 app.config 文件片段.

Below is my app.config file snippet of host windows service.

<services>
      <service name="Share.Services.MainService">
        <endpoint address="net.tcp://localhost:8001/MainService" behaviorConfiguration="netTcpBehavior" binding="netTcpBinding" contract="Share.Services.IClaimService" />
      </service>
      <service name="Share.Services.MainMasterPageService">
        <endpoint address="net.tcp://localhost:8001/MainMasterPageService" behaviorConfiguration="netTcpBehavior" binding="netTcpBinding" contract="Share.Services.IMainMasterpageService" />
      </service>
      <service name="Share.Services.SMSService">
        <endpoint address="net.tcp://localhost:8001/SMSService" behaviorConfiguration="netTcpBehavior" binding="netTcpBinding" contract="Share.ServiceContracts.Messaging.ISMSService" />
      </service>
    </services>

有 3 个 wcf 服务配置为使用 TCP 端点和端口 8001.在 Windows 服务中,我使用以下代码注册服务主机

There are 3 wcf services configured to use TCP endpoint with port 8001. In windows service I am using below code to register Service hosts

private static ServiceHost[] Hosts = null;

public static void Start()
{     
    try
    {
        while (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
        {
            System.Threading.Thread.Sleep(1000);
        }
        BaseLog.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        Hosts = new ServiceHost[] 
            {
                new ServiceHost(typeof(MainService)), 
                new ServiceHost(typeof(MainMasterPageService)),
                new ServiceHost(typeof(SMSService)) 
            };

        foreach (ServiceHost host in Hosts)
        {
            RegisterServiceHost(host);
        }

        _log.Info("All Hosts Open");
    }
    catch(Exception e)
    {
        _log.Error( "MainServiceHost", e);
    }
}

对于每一个我调用RegisterServiceHost函数的ServiceHost对象,该函数的代码如下

For every ServiceHost object I am calling RegisterServiceHost function,code of this function is as below

public static void RegisterServiceHost(ServiceHost host)
{
    var ops = (from e in host.Description.Endpoints
            from o in e.Contract.Operations
            select o).ToList();

    ops.ForEach(
        operation => operation.Behaviors.Add(new MainContextOperationBehavior())
            );

    host.Open();
}

以上代码运行正常,没有任何问题.我的问题是所有服务都共享同一个端口 8001.所有服务如何共享同一个端口.甚至在机器上也没有启用 Net.TCP 端口共享服务.我的另一个问题是共享同一个端口会不会对性能造成任何影响.如果我为每个服务提供像 8001,8002 ,8003 这样的唯一端口,那么它的优势是什么.

Above code is working correctly without any problem. My question is that all services are sharing same port 8001. How all services able to share same port. Even Net.TCP Port Sharing Service is not enabled on the machine. My other question is that will it cause any performance impact in sharing same port. IF I give unique port like 8001,8002 ,8003 for each service then what are the advantage of it.

推荐答案

它们可以共享同一个端口,因为它们都有不同的路径.显然一切正常,所以 WCF 主机足够聪明,可以弄清楚如何让它们在端口 8001 上共享相同的侦听套接字.然后它可以区分请求,因为请求将包含作为 WCF 一部分的服务名称端点配置.

They can share the same port because they've all got different paths. Clearly it all works, so the WCF host is intelligent enough to figure out how to get them to share the same listen socket on port 8001. It can then distinguish between requests, because the requests will include the service name which is part of the WCF endpoint configuration.

我不认为这会导致任何性能问题,但这完全取决于 WCF 服务主机的工作方式.

I wouldn't anticipate this would cause any performance problems, but that does depend entirely on how the WCF service host works.

这篇关于在一个 Windows 服务中的单个 TCP 端口上托管多个 WCF 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆