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的监听地址与监听模式的更多相关文章

  1. 【WCF】终结点的监听地址

    终结点主要作用是向客户端公开一些信息入口,通过这个入口,可以找到要调用的服务操作.通常,终结点会使用三个要素来表述,我记得老蒋(网名:Artech,在园子里可以找到他)在他有关WCF的书里,把这三要素 ...

  2. Linux下修改Oracle监听地址

    如果你的服务器换了ip怎么办? 如果你的服务器换了名字怎么办? 以前的小伙伴怎么办? 以前的老客户怎么办? 没关系,简单教你修改监听地址,老朋友随便找! 想要修改监听地址首先要找到两个文件,确定两样东 ...

  3. webview缓存及跳转时截取url地址、监听页面变化

    缓存及一些设定 我在做一些项目时,h5做的项目手机浏览器能使用,但是在搬到webview时候不能用,这个时候通过查阅资料,原来是webview没有设定好,包括缓存.缓存大小及路径等等 mWebview ...

  4. ASP.NET Core 发布之后通过命令控制监听地址和环境变量

    添加Command支持 新建一个ASP.NET Core 项目,打开Program.cs 添加下面的代码: public class Program { public static void Main ...

  5. Oracle LISTENER 主机名修改为IP地址后LISTENER无法监听到实例 oracle监听错误与hosts文件配置

    为什么listener.ora文件里面HOST后面到底应该输入IP地址还是主机名.我的经验告诉我,这边最好使用主机名.很多的时候,一个机器绑定的不只一个IP地址,如HOST后面是IP地址,那么ORAC ...

  6. 关于.net core 在docker中监听地址设置踩坑记

    1.今天在做docker容器的时候发现如果将.net core 内部监听地址设置为localhost:8888. 2.在docker build -p 6444:8888 运行容器后,外部通过6444 ...

  7. oracle 11g rac修改监听端口(远程监听和本地监听)

    转至:https://www.cnblogs.com/yj411511/p/12459533.html 目录 1.修改远程监听端口 1.1 查看远程监听状态 1.2 修改SCAN listener端口 ...

  8. Oracle静态监听与动态监听概念全解析

    基于11g,linux5.5做出的测试,单实例数据库做出的测试. 1.注册 Instance到监听器去注册自己的Instance_name与ORACLE_HOME,还可以选择添加global_dbna ...

  9. vue watch 深度监听以及立即监听

    vue watch对象可以监听数据,数据发生变化,处理函数 watch虽可以监听,但只是浅监听,只监听数据第一层或者第二层.比如对于整个对象的监听,需要用到深度监听 vm.$watch('obj',f ...

随机推荐

  1. 使用RazorGenerator和预编译MVC引擎将Razor视图编译成DLL

    Web开发中常常会有跨页面.跨站点.跨项目组的复用模块(界面),最常见的就是如下方所示的Web页面上用于显示登录或用户名的头部模块, 使用ASP.NET MVC开发中,常见的做法是写成部分视图,本文的 ...

  2. Android 获取模拟器与真机数据库

    模拟器: localuser:~ localhost$ adb shell shell@android:/ $ su // 数据库复制到 Download 下 shell@android:/ # cp ...

  3. java学习笔记—HTTP协议(10)

    客户端浏览器和服务器Tomcat要进行基本的请求和响应的处理. 其实浏览器和服务器通信的数据的格式已经使用协议进行固定好了,只要双方发送固定格式的数据,那么就可以完成基本的通信. 如果开发者需要查看这 ...

  4. “全栈2019”Java多线程第十三章:线程组ThreadGroup详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  5. “全栈2019”Java异常第十六章:Throwable详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  6. JavaScript求数组Array的并集(javascript面试常见题目)

    var Utils = { joinArray:function(source,target){ for(var i = 0;i<source.length;i++){ var oa = sou ...

  7. 前后端分离——token超时刷新策略

    前言 记录一下前后端分离下————token超时刷新策略! 需求场景 昨天发了一篇记录 前后端分离应用——用户信息传递 中介绍了token认证机制,跟几位群友讨论了下,有些同学有这么一个疑惑:toke ...

  8. 利用python 学习数据分析 (学习一)

    内容学习自: Python for Data Analysis, 2nd Edition         就是这本 纯英文学的很累,对不对取决于百度翻译了 前情提要: 各种方法贴: https://w ...

  9. leetcode-200-岛屿的个数(dfs找所有的连通分量)

    题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...

  10. Vue-cli 2.9 多页配置及多页面之间的跳转问题

    vue开发,现在大部分做的都是(SPA)应用,但是,由于,需求不同,我们针对的用户需求变更较为,频繁,如果每次都全量打包更新,给开发的自测,及测试妹子的任务就会多,每次都要重新验证一下才放心.所以,想 ...