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平台下最大的通信框架.通过终结点承载了所有通信功能.所以终结点的作用将非常重要. ...
随机推荐
- bat windows10系统垃圾清理---
@echo off color 0a title windows10系统垃圾清理--- echo ★☆ ★☆ ★☆ ★☆ ★☆★☆★☆ ★☆ ★☆ ★☆ ★☆★ echo ★☆ ★☆ ★☆ ★☆ ★☆ ...
- shell脚本报错:-bash: xxx: /bin/bash^M: bad interpreter: No such file or directory
当我们把文件从windows系统中编辑的文件拷贝到linux系统中,如果我们执行文件会保存如下的错: shell脚本报错:-bash: xxx: /bin/bash^M: bad interprete ...
- “全栈2019”Java第九十三章:内部类应用场景(迭代器设计模式)
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- 删除标注关联仿dda命令DIMDISASSOCIATE
static void sk_ARXTestXDatamydimassoc(void) { // Add your code for command sk_ARXTestXData.mydimasso ...
- MySQL之FOUND_ROWS()的用法
SELECT FOUND_ROWS() 输出8(输出该表共有多少列)
- java使用Redis8--3.0集群
Redis集群至少需要3个主节点 # cd /usr/redis 创建一个目录 # mkdir cluster # cd cluster 1.复制一个配置文件 # cp ../redis.conf 9 ...
- day03 --class --homework
'''# >>>>>>2 :,有字符串s = "123a4b5c"#>>>>>^ 1: # 1)通过对s切片形成新 ...
- PHP错误——Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)
解释是可用内存已耗尽,这关系到PHP的memory_limit的设置问题. 这里有两种方法解决 1.修改php.ini memory_limit = 128 打开终端输入下列bash命令 cd /pr ...
- Kafka入门学习--基础
Kafka是什么 Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性就可 ...
- C#-WebForm-GridView表格展示数据
GrideView 控件,功能是将数据库的数据用表格的形式展示在页面上 一.<源>代码中放入 GridView 控件 打开<设计>界面 二.绑定数据源 (一)创建 LinQ 类 ...