WCF-ServiceEndpoint的监听地址与监听模式
ServiceEndpoint具有一个可读可写的ListenUri属性,该属性表示服务端终结点的物理监听地址,该地址默认和终结点逻辑地址一致(即ServiceEndpoint的Uri)。对于客户端来说,请求真正发送的目标地址是服务的监听地址,默认情况下终结点的逻辑地址和监听地址是一样的。监听地址可以通过ServiceHost的AddServiceEndpoint指定。
public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, string address);
public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, Uri address);
public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, string address, Uri listenUri);
public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, Uri address, Uri listenUri);
在实际的运行中,ServiceEndpoint的最终监听地址还要由ListenUriMode一起控制。ListenUriModel定义如下:
public enum ListenUriMode
{
Explicit = ,
Unique = ,
}
Explicit表示终结点的监听地址严格采用ListenUri指定设置的Uri,Unique会根据终结点采用的策略确定最终使用的监听地址。
在采用Unique的情况下,监听器地址的生成策略如下:
1.采用Tcp协议并且不采用端口共享,会选择一个未使用的端口作为保证监听地址的唯一性。
2.采用Tcp协议并且采用端口共享,会在原有的监听地址后面加一个Guid以保证监听地址的唯一性。
3.采用非Tcp协议,会在原有的监听地址后面加一个Guid以保证监听地址的唯一性。
下面代码演示服务端是如何确定终结点的监听地址,监听地址在信道分发器(ChannelDispatcher)中体现,
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
namespace host
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(mywcf.Calculator)))
{
ServiceEndpoint endpoint1 = host.AddServiceEndpoint(typeof(mywcf.ICalculator), new WSHttpBinding(), "http://localhost:1111"); ServiceEndpoint endpoint2 = host.AddServiceEndpoint(typeof(mywcf.ICalculator), new WSHttpBinding(), "http://localhost:2222");
endpoint2.ListenUriMode = ListenUriMode.Unique; ServiceEndpoint endpoint3 = host.AddServiceEndpoint(typeof(mywcf.ICalculator), new NetTcpBinding(), "net.tcp://localhost:3333");
endpoint3.ListenUriMode = ListenUriMode.Unique; NetTcpBinding tcpbind = new NetTcpBinding();
tcpbind.PortSharingEnabled = true;
ServiceEndpoint endpoint4 = host.AddServiceEndpoint(typeof(mywcf.ICalculator), tcpbind, "net.tcp://localhost:4444");
endpoint4.ListenUriMode = ListenUriMode.Unique; ServiceEndpoint endpoint5 = host.AddServiceEndpoint(typeof(mywcf.ICalculator), new NetTcpBinding(), "net.tcp://localhost:5555");
host.Opened += delegate { Console.WriteLine("Service Start!"); };
host.Open(); foreach (var dispatch in host.ChannelDispatchers)
{
Console.WriteLine(dispatch.Listener.Uri);
}
}
}
}
}
输出:
endpoint1的监听地址采用默认值,与逻辑地址一致。endpoint2使用了Unique模式,并且是非tcp协议类型,在默认的前提下添加一个GUID。endpoint3使用了Unique模式,并且是tcp协议类型,运行是分配了一个没有被使用的端口。endpoint4采用了端口共享模式,并且是tcp协议类型,监听地址加一个GUID。endpoint5监听地址默认与逻辑地址一致。
示例代码也可以用配置文件方式演示,配置文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding portSharingEnabled="True" name="tcpbind"></binding>
</netTcpBinding>
</bindings>
<services>
<service name="mywcf.Calculator">
<endpoint contract="mywcf.ICalculator" binding="wsHttpBinding" address="http://localhost:1111"></endpoint>
<endpoint contract="mywcf.ICalculator" binding="wsHttpBinding" address="http://localhost:2222" listenUriMode="Unique"></endpoint>
<endpoint contract="mywcf.ICalculator" binding="netTcpBinding" address="net.tcp://localhost:3333" ></endpoint>
<endpoint contract="mywcf.ICalculator" binding="netTcpBinding" address="net.tcp://localhost:4444" listenUriMode="Unique"></endpoint>
<endpoint contract="mywcf.ICalculator" binding="netTcpBinding" bindingConfiguration="tcpbind" address="net.tcp://localhost:5555" listenUriMode="Unique" ></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
采用了配置文件后服务端代码就很简单了,如下:
using System.ServiceModel;
namespace hostxml
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(mywcf.Calculator)))
{
host.Opened += delegate { Console.WriteLine("Service Start!"); };
host.Open();
foreach (var dispatch in host.ChannelDispatchers)
{
Console.WriteLine(dispatch.Listener.Uri);
}
}
}
}
}
WCF-ServiceEndpoint的监听地址与监听模式的更多相关文章
- 【WCF】终结点的监听地址
终结点主要作用是向客户端公开一些信息入口,通过这个入口,可以找到要调用的服务操作.通常,终结点会使用三个要素来表述,我记得老蒋(网名:Artech,在园子里可以找到他)在他有关WCF的书里,把这三要素 ...
- Linux下修改Oracle监听地址
如果你的服务器换了ip怎么办? 如果你的服务器换了名字怎么办? 以前的小伙伴怎么办? 以前的老客户怎么办? 没关系,简单教你修改监听地址,老朋友随便找! 想要修改监听地址首先要找到两个文件,确定两样东 ...
- webview缓存及跳转时截取url地址、监听页面变化
缓存及一些设定 我在做一些项目时,h5做的项目手机浏览器能使用,但是在搬到webview时候不能用,这个时候通过查阅资料,原来是webview没有设定好,包括缓存.缓存大小及路径等等 mWebview ...
- ASP.NET Core 发布之后通过命令控制监听地址和环境变量
添加Command支持 新建一个ASP.NET Core 项目,打开Program.cs 添加下面的代码: public class Program { public static void Main ...
- Oracle LISTENER 主机名修改为IP地址后LISTENER无法监听到实例 oracle监听错误与hosts文件配置
为什么listener.ora文件里面HOST后面到底应该输入IP地址还是主机名.我的经验告诉我,这边最好使用主机名.很多的时候,一个机器绑定的不只一个IP地址,如HOST后面是IP地址,那么ORAC ...
- 关于.net core 在docker中监听地址设置踩坑记
1.今天在做docker容器的时候发现如果将.net core 内部监听地址设置为localhost:8888. 2.在docker build -p 6444:8888 运行容器后,外部通过6444 ...
- oracle 11g rac修改监听端口(远程监听和本地监听)
转至:https://www.cnblogs.com/yj411511/p/12459533.html 目录 1.修改远程监听端口 1.1 查看远程监听状态 1.2 修改SCAN listener端口 ...
- Oracle静态监听与动态监听概念全解析
基于11g,linux5.5做出的测试,单实例数据库做出的测试. 1.注册 Instance到监听器去注册自己的Instance_name与ORACLE_HOME,还可以选择添加global_dbna ...
- vue watch 深度监听以及立即监听
vue watch对象可以监听数据,数据发生变化,处理函数 watch虽可以监听,但只是浅监听,只监听数据第一层或者第二层.比如对于整个对象的监听,需要用到深度监听 vm.$watch('obj',f ...
随机推荐
- 浅析Object基类提供的Equals方法
当我们去查看object.cs源代码文件的时候,会发现object基类提供了三种判断相等性的方法.弄清楚每种方法存在的原因,也就是具体解决了什么问题,对我们理解.net判断对象相等性的逻辑很有帮助,下 ...
- UWP开发砸手机系列(一)—— Accessibility
因为今天讨论的内容不属于入门系列,所以我把标题都改了.这个啥Accessibility说实话属于及其蛋疼的内容,即如何让视力有障碍的人也能通过声音来使用触屏手机……也许你这辈子也不会接触,但如果有一天 ...
- C博客第01次作业---顺序,分支结构
1.本章学习总结 1.1 思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 经过了这一周的学习,从一开始对C语言一无所知,到现在能够写出基本的代码,感到非常开心. 学习C语言也并非想象 ...
- [JXOI2017]颜色 线段树求点对贡献
[JXOI2017]颜色 题目链接 https://www.luogu.org/problemnew/show/P4065 题目描述 可怜有一个长度为 n 的正整数序列 Ai,其中相同的正整数代表着相 ...
- 回去看linux的指令1
SYNC CL : MSM8953 @ CL#:12212299 PROJECT PATH : // Platform / N / NILE / COMBINATION / MSM8953 Cross ...
- iOS开发网络篇—GET请求和POST请求的说明与比较
1.GET请求和POST请求简单说明 1.1 创建GET请求 // 1.设置请求路径 NSString *urlStr = [NSString stringWithFormat:@"http ...
- Code Chef January Challenge 2019题解
传送门 \(div2\)那几道题不来做了太水了-- \(DPAIRS\) 一个显然合法的方案:\(A\)最小的和\(B\)所有连,\(A\)剩下的和\(B\)最大的连 算了咕上瘾了,咕咕咕 const ...
- [Flex] 组件Tree系列 —— 作为PopUpButton的弹出菜单
mxml: <?xml version="1.0" encoding="utf-8"?> <!--功能描述:Tree作为PopUpButton ...
- git 命令摘录
回滚 n 个 commit (增加了revert commit) git revert -n commit_id 回滚到指定的commit_id(不增加commit,回滚的commit_id被删除) ...
- 插入排序 思想 JAVA实现
已知一个数组 60.28.41.39.6 .18 .14.28.49.31 利用插入排序算法进行排序 插入排序是一个运行时间为O(N²)的排序算法. 算法思想 60.28.41.39.6 .18 . ...