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平台下最大的通信框架.通过终结点承载了所有通信功能.所以终结点的作用将非常重要. ...
随机推荐
- VS2015 IIS Express 无法启动 解决办法(转)
因为安装各种乱七八糟的软件,然后不小心把IIS Express卸载掉了,网上下载了一个IIS Express 7,安装之后本地使用VS 2015无法启动调试,F5 无法启动IIS, 再次F5调试,没有 ...
- iOS 添加字体
1. 将字体(ttf 文件)导入项目. 2. 在项目plist 文件里的 Fonts provided by application 添加新导入的字体. 3. 代码中的调用 [aLabel setFo ...
- sql server中的 trimtrailingblanks
使用sp_help 查出 发现有个这个属性, 如何修改呢? SET ANSI_PADDING ONAlter Table Sys_users_History Alter column PveSit ...
- 初学python之路-day15
一.生成器send方法 # send的工作原理 # 1.send发送信息给当前停止的yield # 2.再去调用__next__()方法,生成器接着往下指向,返回下一个yield值并停止 # 案例: ...
- LOJ#6038. 「雅礼集训 2017 Day5」远行(LCT)
题面 传送门 题解 要不是因为数组版的\(LCT\)跑得实在太慢我至于去学指针版的么--而且指针版的完全看不懂啊-- 首先有两个结论 1.与一个点距离最大的点为任意一条直径的两个端点之一 2.两棵树之 ...
- 阿里云ros实例
模板文件 { "ROSTemplateFormatVersion": "2015-09-01", "Parameters": { " ...
- Learn to See in the Dark(论文阅读笔记)
最近做项目看了一篇论文<Learn to See in the Dark>下面是一些论文笔记 概括: 这篇论文主要介绍的是在低光照的环境下用两个标准的FCN网络,通过控制变量法来对比不同的 ...
- jenkins运行Python
法一: 配置中构建执行Windows批处理命令如下 立即构建后,报错如下,提示python 不是内部或外部指令 修改Windows批处理指令如下: 再次“立即构建”则正常 法二: 安装Python插件 ...
- 南昌网络赛 Distance on the tree 主席树+树剖 (给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数。)
https://nanti.jisuanke.com/t/38229 题目: 给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数. #include <bits/stdc++.h ...
- mysql数据库基本知识
一.库操作 创建数据库:creat database 'mydababase1';creat database if not exists 'mydababase1' //只有两个选项 查询数据库: ...