当前位置: 首页 > news >正文

WCF-终结点之消息路由示例

一、 

在前一章中主要介绍了服务端的监听地址与逻辑地址。本节模拟消息转发机制来实际体验一把终结点的监听地址是如何使用的。

先下载一个叫做TcpTrace的小软件(108k),它能够截取端口消息,并转发消息。

先来看看服务端代码,服务端的绑定使用WS2007HttpBinding的不加密模式,因为我们等会还要看TcpTrace捕获的数据明文,所以不能加密。客户端也是使用不加密的WS2007HttpBinding。

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)))
            {
                WS2007HttpBinding bind = new WS2007HttpBinding();
                WSHttpSecurity security = new WSHttpSecurity();
                security.Mode = SecurityMode.None;
                bind.Security = security;
                host.AddServiceEndpoint(typeof(mywcf.ICalculator), bind, "http://localhost:8888");
                host.Opened += delegate { Console.WriteLine("Service Start!"); };
                host.Open();
                Console.ReadLine();
            }
        }
    }
}

 服务端发布一个终结点。由前一章说道,默认逻辑地址与监听地址一致,都是http://localhost:8888。

客户端代码如下:

using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
namespace client
{
    class Program
    {
        static void Main(string[] args)
        {
            WS2007HttpBinding bind = new WS2007HttpBinding();
            WSHttpSecurity security = new WSHttpSecurity();
            security.Mode = SecurityMode.None;
            bind.Security = security;
            EndpointAddress endpint=new EndpointAddress("http://localhost:8888");
            mywcf.ICalculator client = ChannelFactory<mywcf.ICalculator>.CreateChannel(bind, endpint, new Uri("http://localhost:9999"));
            Console.WriteLine(client.Add(1, 2));
        }
    }
}

 先启动服务端,再启动客户端。客户端报了一个错误,无法找到正确的终结点。

客户端的访问的逻辑地址为http://localhost:8888,监听地址为http://localhost:9999。但是服务端监听的是8888,并没有监听9999,所以访问失败。我们现在利用TcpTrace工具,把服务端9999端口收到的消息转发至服务端8888端口,服务端就能够正确的接收到请求。

点击OK,现在再次先后启动服务端和客户端,访问成功,并且已经成功转发。

现在分析一下消息内容。客户端请求<To>报头为逻辑地址为localhost:8888,目标地址为localhost:9999,<Body>为x=1,y=2,请求Add操作。下面服务端返回成功,<AddResult>为3。正因为客户端的<To>逻辑地址与服务端的逻辑地址一致,才可以通过终结点的消息筛选器。

 

二、

若服务端代码不变,服务端的逻辑地址和监听地址依然是8888,现在将客户端的逻辑地址和监听地址都设置为9999,再通过TcpTrace转发,将9999端口消息转发到8888端口。

客户端代码如下:

using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
namespace client
{
    class Program
    {
        static void Main(string[] args)
        {
            WS2007HttpBinding bind = new WS2007HttpBinding();
            WSHttpSecurity security = new WSHttpSecurity();
            security.Mode = SecurityMode.None;
            bind.Security = security;
            EndpointAddress endpint=new EndpointAddress("http://localhost:9999");
            mywcf.ICalculator client = ChannelFactory<mywcf.ICalculator>.CreateChannel(bind, endpint);
            Console.WriteLine(client.Add(1, 2));
        }
    }
}

 先后运行客户端与服务端,发现报了一个异常。

再来看看TcpTrace截取的请求消息:

 

<To>报头地址的端口变成了9999。即使TcpTrace进行了转发,因为逻辑地址不匹配,这样的消息无法通过服务端终结点的地址筛选器。

三、
如果想让上述客户端能够正常访问服务端。在自定义的类库的服务行为将AddressFilterMode设置成AddressFilterMode.Any,即使请求的<To>逻辑地址与服务端终结点逻辑地址不一致也可以访问。

using System.ServiceModel;
namespace mywcf
{
    [ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
    public class Calculator : ICalculator
    {
        public int Add(int x, int y)
        {
            return x + y;
        }
    }
}

 

 四、

   客户端访问的监听地址也可以通过配置文件进行配置。配置如下,通过clientVia标签将监听地址端口设置为9999。逻辑地址的端口是8888。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="bindbehavior">
          <clientVia viaUri="http://localhost:9999"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <client>
      <endpoint  address="http://localhost:8888" binding="wsHttpBinding"  contract="mywcf.ICalculator" behaviorConfiguration="bindbehavior" name="myendpoint"></endpoint>
    </client>
  </system.serviceModel>
</configuration>

 

转载于:https://www.cnblogs.com/lh218/p/4379528.html

相关文章:

  • Android学习笔记(四六):互联网通信-文件下载
  • 封装WebService的APM为Async、Await模式利于Asp.Net页面调用
  • Solr入门之SolrServer实例化方式
  • matlab mex 小o -o 出错
  • linux下的权限控制
  • java项目在linux上的运行
  • Dev的WPF控件与VS2012不兼容问题
  • [转]ARM Linux 3.x Device Tree Usage
  • AutoFac使用方法总结
  • malloc/free和new/delete的异同
  • java 下载spring的方法
  • Delphi项目构成之窗体文件(Form Files).DFM
  • quartz定时任务框架调度机制解析
  • 图片文件,图片文件流和BASE64加密字符串之间的转换,以及图片的BASE64加密字符串再jsp上如何显示...
  • 字符串处理函数的自定义函数实现
  • emacs初体验
  • express.js的介绍及使用
  • HomeBrew常规使用教程
  • HTTP--网络协议分层,http历史(二)
  • Idea+maven+scala构建包并在spark on yarn 运行
  • JavaScript 基础知识 - 入门篇(一)
  • javascript从右向左截取指定位数字符的3种方法
  • MySQL常见的两种存储引擎:MyISAM与InnoDB的爱恨情仇
  • PV统计优化设计
  • STAR法则
  • windows-nginx-https-本地配置
  • Yeoman_Bower_Grunt
  • 浮动相关
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 设计模式(12)迭代器模式(讲解+应用)
  • 我与Jetbrains的这些年
  • 想使用 MongoDB ,你应该了解这8个方面!
  • Prometheus VS InfluxDB
  • raise 与 raise ... from 的区别
  • zabbix3.2监控linux磁盘IO
  • 阿里云服务器如何修改远程端口?
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • ​LeetCode解法汇总518. 零钱兑换 II
  • #每日一题合集#牛客JZ23-JZ33
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (SpringBoot)第七章:SpringBoot日志文件
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .gitignore文件_Git:.gitignore
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • .NET程序员迈向卓越的必由之路
  • .net下的富文本编辑器FCKeditor的配置方法
  • .NET中的十进制浮点类型,徐汇区网站设计
  • [ CTF ] WriteUp- 2022年第三届“网鼎杯”网络安全大赛(白虎组)
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——