1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using System.ServiceModel;
  8. using System.ServiceModel.Dispatcher;
  9. using System.ServiceModel.Description;
  10. using System.ServiceModel.Channels;
  11.  
  12. namespace MyLib
  13. {
  14. /// <summary>
  15. /// 消息拦截器
  16. /// </summary>
  17. public class MyMessageInspector:IClientMessageInspector,IDispatchMessageInspector
  18. {
  19. void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
  20. {
  21. Console.WriteLine("客户端接收到的回复:\n{0}", reply.ToString());
  22. }
  23.  
  24. object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
  25. {
  26. Console.WriteLine("客户端发送请求前的SOAP消息:\n{0}", request.ToString());
  27. return null;
  28. }
  29.  
  30. object IDispatchMessageInspector.AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
  31. {
  32. Console.WriteLine("服务器端:接收到的请求:\n{0}", request.ToString());
  33. return null;
  34. }
  35.  
  36. void IDispatchMessageInspector.BeforeSendReply(ref Message reply, object correlationState)
  37. {
  38. Console.WriteLine("服务器即将作出以下回复:\n{0}", reply.ToString());
  39. }
  40. }
  41.  
  42. /// <summary>
  43. /// 插入到终结点的Behavior
  44. /// </summary>
  45. public class MyEndPointBehavior : IEndpointBehavior
  46. {
  47. public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
  48. {
  49. // 不需要
  50. return;
  51. }
  52.  
  53. public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  54. {
  55. // 植入“偷听器”客户端
  56. clientRuntime.ClientMessageInspectors.Add(new MyMessageInspector());
  57. }
  58.  
  59. public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
  60. {
  61. // 植入“偷听器” 服务器端
  62. endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new MyMessageInspector());
  63. }
  64.  
  65. public void Validate(ServiceEndpoint endpoint)
  66. {
  67. // 不需要
  68. return;
  69. }
  70. }
  71.  
  72. }

  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using System.Runtime;
  8. using System.Runtime.Serialization;
  9. using System.ServiceModel;
  10. using System.ServiceModel.Description;
  11.  
  12. namespace WCFServer
  13. {
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. // 服务器基址
  19. Uri baseAddress = new Uri("http://localhost:1378/services");
  20. // 声明服务器主机
  21. using (ServiceHost host = new ServiceHost(typeof(MyService), baseAddress))
  22. {
  23. // 添加绑定和终结点
  24. WSHttpBinding binding = new WSHttpBinding();
  25. host.AddServiceEndpoint(typeof(IService), binding, "/test");
  26. // 添加服务描述
  27. host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
  28. // 把自定义的IEndPointBehavior插入到终结点中
  29. foreach (var endpont in host.Description.Endpoints)
  30. {
  31. endpont.EndpointBehaviors.Add(new MyLib.MyEndPointBehavior());
  32. }
  33. try
  34. {
  35. // 打开服务
  36. host.Open();
  37. Console.WriteLine("服务已启动。");
  38. }
  39. catch (Exception ex)
  40. {
  41. Console.WriteLine(ex.Message);
  42. }
  43. Console.ReadKey();
  44. }
  45. }
  46. }
  47.  
  48. [ServiceContract(Namespace = "MyNamespace")]
  49. public interface IService
  50. {
  51. [OperationContract]
  52. int AddInt(int a, int b);
  53. [OperationContract]
  54. Student GetStudent();
  55. [OperationContract]
  56. CalResultResponse ComputingNumbers(CalcultRequest inMsg);
  57. }
  58.  
  59. [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
  60. public class MyService : IService
  61. {
  62. public int AddInt(int a, int b)
  63. {
  64. return a + b;
  65. }
  66.  
  67. public Student GetStudent()
  68. {
  69. Student stu = new Student();
  70. stu.StudentName = "小明";
  71. stu.StudentAge = 22;
  72. return stu;
  73. }
  74.  
  75. public CalResultResponse ComputingNumbers(CalcultRequest inMsg)
  76. {
  77. CalResultResponse rmsg = new CalResultResponse();
  78. switch (inMsg.Operation)
  79. {
  80. case "加":
  81. rmsg.ComputedResult = inMsg.NumberA + inMsg.NumberB;
  82. break;
  83. case "减":
  84. rmsg.ComputedResult = inMsg.NumberA - inMsg.NumberB;
  85. break;
  86. case "乘":
  87. rmsg.ComputedResult = inMsg.NumberA * inMsg.NumberB;
  88. break;
  89. case "除":
  90. rmsg.ComputedResult = inMsg.NumberA / inMsg.NumberB;
  91. break;
  92. default:
  93. throw new ArgumentException("运算操作只允许加、减、乘、除。");
  94. break;
  95. }
  96. return rmsg;
  97. }
  98. }
  99.  
  100. [DataContract]
  101. public class Student
  102. {
  103. [DataMember]
  104. public string StudentName;
  105. [DataMember]
  106. public int StudentAge;
  107. }
  108.  
  109. [MessageContract]
  110. public class CalcultRequest
  111. {
  112. [MessageHeader]
  113. public string Operation;
  114. [MessageBodyMember]
  115. public int NumberA;
  116. [MessageBodyMember]
  117. public int NumberB;
  118. }
  119.  
  120. [MessageContract]
  121. public class CalResultResponse
  122. {
  123. [MessageBodyMember]
  124. public int ComputedResult;
  125. }
  126. }

  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace WCFClient
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. WS.ServiceClient client = new WS.ServiceClient();
  14. // 记得在客户端也要插入IEndPointBehavior
  15. client.Endpoint.EndpointBehaviors.Add(new MyLib.MyEndPointBehavior());
  16. try
  17. {
  18. // 1、调用带元数据参数和返回值的操作
  19. Console.WriteLine("\n20和35相加的结果是:{0}", client.AddInt(20, 35));
  20. // 2、调用带有数据协定的操作
  21. WS.Student student = client.GetStudent();
  22. Console.WriteLine("\n学生信息---------------------------");
  23. Console.WriteLine("姓名:{0}\n年龄:{1}", student.StudentName, student.StudentAge);
  24. // 3、调用带消息协定的操作
  25. Console.WriteLine("\n15乘以70的结果是:{0}", client.ComputingNumbers("乘", 15, 70));
  26. }
  27. catch (Exception ex)
  28. {
  29. Console.WriteLine("异常:{0}", ex.Message);
  30. }
  31.  
  32. client.Close();
  33. Console.ReadKey();
  34. }
  35. }
  36. }

  

  1. /// <summary>
  2. /// 消息拦截器
  3. /// </summary>
  4. public class MyMessageInspector:IClientMessageInspector,IDispatchMessageInspector
  5. {
  6. void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
  7. {
  8. //Console.WriteLine("客户端接收到的回复:\n{0}", reply.ToString());
  9. return;
  10. }
  11.  
  12. object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
  13. {
  14. //Console.WriteLine("客户端发送请求前的SOAP消息:\n{0}", request.ToString());
  15. // 插入验证信息
  16. MessageHeader hdUserName = MessageHeader.CreateHeader("u", "fuck", "admin");
  17. MessageHeader hdPassWord = MessageHeader.CreateHeader("p", "fuck", "");
  18. request.Headers.Add(hdUserName);
  19. request.Headers.Add(hdPassWord);
  20. return null;
  21. }
  22.  
  23. object IDispatchMessageInspector.AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
  24. {
  25. //Console.WriteLine("服务器端:接收到的请求:\n{0}", request.ToString());
  26. // 栓查验证信息
  27. string un = request.Headers.GetHeader<string>("u", "fuck");
  28. string ps = request.Headers.GetHeader<string>("p", "fuck");
  29. if (un == "admin" && ps == "abcd")
  30. {
  31. Console.WriteLine("用户名和密码正确。");
  32. }
  33. else
  34. {
  35. throw new Exception("验证失败,滚吧!");
  36. }
  37. return null;
  38. }
  39.  
  40. void IDispatchMessageInspector.BeforeSendReply(ref Message reply, object correlationState)
  41. {
  42. //Console.WriteLine("服务器即将作出以下回复:\n{0}", reply.ToString());
  43. return;
  44. }
  45. }

IClientMessageInspector IDispatchMessageInspector的更多相关文章

  1. 重温WCF之消息拦截与篡改(八)

    我们知道,在WCF中,客户端对服务操作方法的每一次调用,都可以被看作是一条消息,而且,可能我们还会有一个疑问:如何知道客户端与服务器通讯过程中,期间发送和接收的SOAP是什么样子.当然,也有人是通过借 ...

  2. WCF消息拦截,利用消息拦截做身份验证服务

    本文参考  http://blog.csdn.net/tcjiaan/article/details/8274493  博客而写 添加对信息处理的类 /// <summary> /// 消 ...

  3. 传说中的WCF(10):消息拦截与篡改

    我们知道,在WCF中,客户端对服务操作方法的每一次调用,都可以被看作是一条消息,而且,可能我们还会有一个疑问:如何知道客户端与服务器通讯过 程中,期间发送和接收的SOAP是什么样子.当然,也有人是通过 ...

  4. WCF不用证书实现验证(messageheader)

    上文WCF进阶:将消息正文Base64编码中介绍了实现自定义MessageInspector来记录消息和实现自定义Formatter来改写消息,本文介绍一下在WCF中使用SoapHeader进行验证的 ...

  5. 传说中的WCF:消息拦截与篡改

    我们知道,在WCF中,客户端对服务操作方法的每一次调用,都可以被看作是一条消息,而且,可能我们还会有一个疑问:如何知道客户端与服务器通讯过程中,期间发送和接收的SOAP是什么样子.当然,也有人是通过借 ...

  6. wcf利用IDispatchMessageInspector实现接口监控日志记录和并发限流

    一般对于提供出来的接口,虽然知道在哪些业务场景下才会被调用,但是不知道什么时候被调用.调用的频率.接口性能,当出现问题的时候也不容易重现请求:为了追踪这些内容就需要把每次接口的调用信息给完整的记录下来 ...

  7. 关于WEB Service&WCF&WebApi实现身份验证之WCF篇(2)

    因前段时间工作变动(换了新工作)及工作较忙暂时中断了该系列文章,今天难得有点空闲时间,就继续总结WCF身份验证的其它方法.前面总结了三种方法(详见:关于WEB Service&WCF& ...

  8. WCF自定义扩展,以实现aop!

    引用地址:https://msdn.microsoft.com/zh-cn/magazine/cc163302.aspx  使用自定义行为扩展 WCF Aaron Skonnard 代码下载位置: S ...

  9. [WCF]设置拦截器捕捉到request和reply消息

    WCF进阶学习ing... 在熟练掌握了ABC的使用以后,就开始想着去了解WCF是怎么通信的了.首先是服务描述语言wsdl,它定义了服务的描述等等,用于让外界知道这个服务的ABC是什么.另外一个比较重 ...

随机推荐

  1. Vulnerability Scanning Tools

    Category:Vulnerability Scanning Tools - OWASP https://www.owasp.org/index.php/Category:Vulnerability ...

  2. DbSet.Attach(实体)与DbContext.Entry(实体).State = EntityState.Modified 区别

    当你使用这个DbSet.Update方法时,实体框架将你实体的所有属性标记为EntityState.Modified,所以跟踪它们.如果你只想更改部分属性,而不是全部属性,请使用DbSet.Attac ...

  3. Python3基础 str __add__ 拼接,原字符串不变

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  4. VPB测试 使用Osgdem运行例子

    1.Osgdem运行例子所需数据下载地址: http://www.cc.gatech.edu/projects/large_models/ps.html Download Elevation Map: ...

  5. 001——Typescript 介绍 、Typescript 安 装、Typescript 开发工具

    一. Typescript 介绍 1. TypeScript 是由微软开发的一款开源的编程语言. 4. TypeScript 是 Javascript 的超级,遵循最新的 ES6.Es5 规范.Typ ...

  6. 手机wifi连上Fiddler后无网络问题解决

    早上老板交代一个任务,对一款app抓包分析下接口调用的时延.我的重新打开了一年多前用过的Fiddler(参见win10笔记本用Fiddler对手机App抓包),拿过测试手机开始设置wifi代理地址和端 ...

  7. 【mysql】添加删除权限

    https://www.cnblogs.com/wuxunyan/p/9095016.html

  8. C#中 IEnumerable, ICollection, IList, List的使用

    List是類,實現了IList接口,IList繼承了ICollection,ICollection繼承了IEnumerable,IEnumerable是其中最底層的接口. 實現IEnumerable接 ...

  9. web端自动化——unittest框架编写web测试用例

    1.前言: 对于初学者来说,python自带的IDLE,精简又方便,不过一个好的编辑器能让python编码变得更方便,更加优美些. 不过呢,也可以自己去下载其他更好用的代码编辑器,在这推荐: PyCh ...

  10. Selenium2+python自动化2.7-火狐44版本环境搭建(转)

    转载地址:https://www.cnblogs.com/yoyoketang/p/selenium.html 前言 目前selenium版本已经升级到3.0了,网上的大部分教程是基于2.0写的,所以 ...