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>
WCF-netTcpBinding端口共享的更多相关文章
- WCF服务启用与配置端口共享
在 Windows Communication Foundation (WCF) 应用程序中使用 net.tcp:// 端口共享的最简单方式是使用 NetTcpBinding 公开一个服务. 此绑定提 ...
- WCF NetTcpBinding 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作
背景:WindowsService + WCF + NetTcpBinding 之前一直使用http协议模式,改为net.tcp之后隔段时间出现:由于系统缓冲区空间不足或队列已满,不能执行套接字上的操 ...
- 如何在IIS中承载WCF NetTcpBinding 服务
这篇博客将介绍如何在IIS中承载NetTcpBinding的服务. 1. 首先准备服务代码. Contract namespace Contract { [ServiceContract] publi ...
- 求关注 wcf bindipendpointdelegate 端口限制的功能
我最近也需要实现一个功能:1)一个客户端(192.168.0.15),10个服务端(提供A接口.B接口)如下: 192.168.0.1-5685 192.168.0.2-5685 ...
- WCF NetTcpBinding
服务端: <system.serviceModel> <bindings> <netTcpBinding> <binding portSharingEnabl ...
- WCF : 如何将NetTcpBinding寄宿在IIS7上
摘要 : 从IIS 7 开始, IIS增加了对非HTTP协议的支持. 因此, 自IIS 7之后, 可以将NetTcpBinding等非HTTP协议的Bindings直接寄宿在IIS上面. 本文将介绍如 ...
- WCF、MongoDB
http://www.cnblogs.com/quietwalk/archive/2011/08/09/2132573.html http://www.cnblogs.com/huangxinchen ...
- 十五天精通WCF——第五天 你需要了解的三个小技巧
一: 服务是端点的集合 当你在开发wcf的时候,你或许已经注意到了一个service可以公布多个endpoint,确实是这样,在wcf中有一句很经典的话,叫做“服务是端点的集合",就 比如说 ...
- [Solution] 一步一步WCF(2) 终结点Endpoint
繁忙的一天又一天,不管其他,先继续WCF吧. Endpoint包含地址,绑定,契约三要素.WCF作为一个Windows平台下最大的通信框架.通过终结点承载了所有通信功能.所以终结点的作用将非常重要. ...
随机推荐
- Java容器中的元素输出
1.容器不同于数组,容器若是想输出全部元素,可以直接利用System.out.println(collection) public class TestCollectionArrayPrint { p ...
- 小白学Linux
Linux的文件关系: / 根最大的文件夹,存储此台计算机的所有数据 /etc 存放计算机的配置文件 /var/log 存放电脑的日志文件 /home 家的位置 路径:相对路径.绝对路径(从根下开始 ...
- jQuery中animate与scrollTop、offset().top实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 50余本中外Python电子教程及源码下载地址
链接:http://pan.baidu.com/s/1c0VTwsC 密码:hapu
- ElasticSearch基本查询
词条查询 这是一个简单查询.它仅 匹配给定字段中包含该词条的稳定,且是2未经分析的确切的词条. { “query” :{ “term”:{ “title”:”crime” } } } 多词条查询 匹配 ...
- 使用deque模块固定队列长度,用headq模块来查找最大或最小的N个元素以及实现一个优先级排序的队列
一. deque(双端队列) 1. 使用 deque(maxlen=N)会新建一个固定大小的队列.当新的元素加入并且这个队列已满的时候,最老的元素会自动被移除掉 >>> from c ...
- 利用python 学习数据分析 (学习二)
内容学习自: Python for Data Analysis, 2nd Edition 就是这本 纯英文学的很累,对不对取决于百度翻译了 前情提要: 各种方法贴: https://w ...
- php prepare
详见 https://stackoverflow.com/questions/134099/are-pdo-prepared-statements-sufficient-to-prevent-sql- ...
- 使用python 模仿mybinlog 命令 二进制分析mysql binlog
出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该声明. ...
- leetcode-39-组合总和(有趣的递归)
题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无 ...