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平台下最大的通信框架.通过终结点承载了所有通信功能.所以终结点的作用将非常重要. ...
随机推荐
- WinForm 窗体应用程序(初步)之三
进程: 进程,简单的说,就是让你的程序启动另一个程序. 1.Process.Start("calc");//启动计算器 弊端:只认识系统自带的程序,如果写错系统会崩溃. 2. // ...
- WebApi Post string 参数 为空
用webApi做开发也有很久了 一些 细节平时可能未必很留心 今天就很奇葩 post 只接受一个string 参数的数据 但接收是一直未空 很奇怪 看了一些资料后得出以下结论
- sgi stl内存池实现------源码加翻译
class __default_alloc_template { enum { unit = 8 };//分配单位 后面直接用8代替 enum { max_bytes = 128 };//最大分配字节 ...
- 常见的vue面试题
001.v-show与v-if的区别v-show:操作的是元素的display属性 v-if:操作的是元素的创建和插入相比较而言v-show的性能要高 002.methods.computed.wat ...
- Ajax请求的参数
post请求和get请求存放参数位置 post请求和get请求存放参数位置是不同的: post方式参数存放在请求数据包的消息体中. get方式参数存放在请求数据包的请求行的URI字段中,以?开始以pa ...
- 设置视口中心点setViewCenter
ads_point pt; ads_name ent,ss; //切换到模型空间 acedMspace(); if (RTNORM != acedGetPoint(NULL,_T("\n选择 ...
- Kali Linux安全渗透-从入门到精通
Kali-Linux是基于Debian Linux发行版 针对高级渗透测试和安全审计系统.带你一起从入门到精通. 什么是Kali-Linux? kali 包含几百个软件用来执行各种信息安全的任务,如渗 ...
- 理解 atime,ctime,mtime (下)
话不多说,开始下篇. # 前言 通过 "理解 atime,ctime,mtime (上)" 我们已经知道了atime 是文件访问时间:ctime是文件权限改变时间:mtime是文件 ...
- Python 魔法方法查询表 -- 总结篇
据说,Python 的对象天生拥有一些神奇的方法,它们总被双下划线所包围,他们是面向对象的 Python 的一切. 他们是可以给你的类增加魔力的特殊方法,如果你的对象实现(重载)了这些方法中的某一个, ...
- 【Quartz】解密properties配置文件中的账号密码
在配置quartz时,为了保密某些信息(特别是账号密码),通常会使用密文.那么在实际使用这些配置信息时,需要进行解密.本文提供一种解密方法如下: (1)假设在properties文件中加密了账号密码 ...