zoukankan      html  css  js  c++  java
  • WCF-netTcpBinding端口共享

      在同一台机器上一个端口在某时刻只能被一个应用程序占用。对于WCF服务来说,如果服务器上有多个服务并且这些服务寄宿在不同的应用程序中,我们需要某种途径来共享它们的端口。下面是一个示例来演示使用TcpBinding进行端口共享。在VS2010中创建两个WCF服务工程,使用TCP绑定并且使用同一个端口进行部署。

    工程1代码如下:                              

    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    namespace hostTcpPortSharing2
    {
        class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host2 = new ServiceHost(typeof(mywcf.CalculatorService));
                NetTcpBinding tcpbind = new NetTcpBinding();
                host2.AddServiceEndpoint(typeof(mywcf.ICalculatorService), tcpbind, "net.tcp://localhost:8899/tcp1");
                host2.Opened += delegate { Console.WriteLine("net.tcp://localhost:8899/tcp1 Service Start!"); };
                host2.Open();
                Console.ReadLine();
            }
        }
    }
    

     工程2代码:

    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    namespace hostTcpPortSharing2
    {
        class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host2 = new ServiceHost(typeof(mywcf.CalculatorService));
                NetTcpBinding tcpbind = new NetTcpBinding();
                host2.AddServiceEndpoint(typeof(mywcf.ICalculatorService), tcpbind, "net.tcp://localhost:8899/tcp2");
                host2.Opened += delegate { Console.WriteLine("net.tcp://localhost:8899/tcp2 Service Start!"); };
                host2.Open();
                Console.ReadLine();
            }
        }
    }
    

     这两个工程代码基本一致,相同点就是都用了NetTcpBinding,并且都部署到8899端口,唯一不同的是终结点地址。先运行工程1,再运行工程2。

    端口已经被工程1占用,工程2无法使用。

    WCF对服务的端口共享提供了支持方法。在端口共享服务开启的状态下,当两个使用同一个端口的服务Open的时候,会注册到Net.Tcp共享服务中。注册内容为匹配地址-宿主进程。当客户端调用的时候会根据请求中的终结点地址转发到对应的服务进程。开启Tcp端口的方法很简单,只需要在TcpBinding的PortSharingEnabled属性设置为true即可。注意,工程1和工程2都需要设置。代码如下:

    NetTcpBinding tcpbind = new NetTcpBinding();
    tcpbind.PortSharingEnabled = true;
    host.AddServiceEndpoint(typeof(mywcf.ICalculatorService), tcpbind, "net.tcp://localhost:8899/tcp1");
    
    NetTcpBinding tcpbind = new NetTcpBinding();
    tcpbind.PortSharingEnabled = true;
    host.AddServiceEndpoint(typeof(mywcf.ICalculatorService), tcpbind, "net.tcp://localhost:8899/tcp2");
    

     如果用配置文件的方式则为:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding name="tccpbind" portSharingEnabled="true"></binding>
          </netTcpBinding>
        </bindings>
        <services>
          <service name="mywcf.CalculatorService">
            <endpoint address="net.tcp://localhost:8899" binding="netTcpBinding" bindingConfiguration="tccpbind" contract="mywcf.ICalculatorService"></endpoint>
          </service>
        </services>
      </system.serviceModel>
    </configuration>
    
  • 相关阅读:
    linux学习(一)
    Linux学习(用户管理)
    anyproxy mac安装
    python mitmproxy 代理
    Js常用方法map, sort
    echarts常用配置项【持续更新】
    【转】moment js 使用
    js Cannot assign to read only property 'exports' of object '#<Object>' at Modul,import 与module.exports混用问题
    a标签跳转referer漏洞
    element ui rate评分组建使用
  • 原文地址:https://www.cnblogs.com/lh218/p/4376727.html
Copyright © 2011-2022 走看看