跟着Artech学习WCF扩展(4) 扩展MessageInspector

简介: 自定义MessageInspector 在client端和service端都有自己的MessageInspector,client端叫做ClientMessageInspector,实现System.ServiceModel.Dispatcher.IClientMessageInspector interface,而service端叫做 DispatchMessageInspector, 实现了System.ServiceModel.Dispatcher.IDispatchMessageInspector interface。

自定义MessageInspector

在client端和service端都有自己的MessageInspector,client端叫做ClientMessageInspector,实现System.ServiceModel.Dispatcher.IClientMessageInspector interface,而service端叫做 DispatchMessageInspector, 实现了System.ServiceModel.Dispatcher.IDispatchMessageInspector interface。DispatchMessageInspector允许你在request message交付到具体的DispatchOperation付诸执行之前或者reply message返回client之前对incoming message/outgoing message进行检验、修改或者其他一些基于message的操作;

IDispatchMessageInspector的定义很简单

public interface IDispatchMessageInspector
{
    // Methods
    object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext);
    void BeforeSendReply(ref Message reply, object correlationState);
}

在实际的项目开发中,MessageInspector使用相当广范,比如:我们可以定义自己的MessageInspector实现Logging功能;或者在Client端通过ClientMessageInspector添加一些与业务无关的context信息,并在service通过DispatchMessageInspector将其取出。在本系列后续的部分我将给你一个application context propagation的具体应用。

剩下的地方在这里:点我

代码下载:点我

Client端对 IClientMessageInspector 进行实现

在service端对IDispatchMessageInspector进行实现

但Artech在service端对 ICallContextInitializer进行了实现

出去学习的目的,拼拼代码 也把IDispatchMessageInspector实现了

不过不知道对不对 忍者  自己拼代码实在是盲人摸像 搞了一天才弄明白,不对是还没弄明白

通过细查 发现这个扩展 实在是太有用了外星人

部分代码如下作为备份

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Artech.ContextPropagation
{
   public class DispatchMessageInspectorTest:System.ServiceModel.Dispatcher.IDispatchMessageInspector
    {
       public bool IsBidirectional
       { get; set; }

       public DispatchMessageInspectorTest()
           : this(false)
       {

       }

       public DispatchMessageInspectorTest(bool isBidirectional)
       {
           this.IsBidirectional = isBidirectional;
       }


 


        //DispatchMessageInspector
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            ApplicationContext context = request.Headers.GetHeader<ApplicationContext>(ApplicationContext.ContextHeaderLocalName, ApplicationContext.ContextHeaderNamespace);
            if (context == null)
            {
                return null;
            }

            ApplicationContext.Current = context;
            return ApplicationContext.Current;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {

            if (!this.IsBidirectional)
            {
                return;
            }

            ApplicationContext context = correlationState as ApplicationContext;
            if (context == null)
            {
                return;
            }
            MessageHeader<ApplicationContext> contextHeader = new MessageHeader<ApplicationContext>(context);
            OperationContext.Current.OutgoingMessageHeaders.Add(contextHeader.GetUntypedHeader(ApplicationContext.ContextHeaderLocalName, ApplicationContext.ContextHeaderNamespace));
            ApplicationContext.Current = null;

        }




    }
}

 

=========

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Artech.ContextPropagation
{
    public class DispatchMessageInspectorTestBehavior : IEndpointBehavior
    {
        public bool IsBidirectional
        { get; set; }


        public DispatchMessageInspectorTestBehavior()
            : this(false)
        {

        }

        public DispatchMessageInspectorTestBehavior(bool isBidirectional)
        {
            this.IsBidirectional = isBidirectional;
        }


        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
          
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(new ContextAttachingMessageInspector(this.IsBidirectional));
       
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {

            DispatchMessageInspectorTest inspector = new DispatchMessageInspectorTest(this.IsBidirectional);
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            
        }
    }
}

 

==============

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Configuration;
using System.Configuration;

namespace Artech.ContextPropagation
{
   public class DispatchMessageInspectorTestElement:BehaviorExtensionElement
    {
       [ConfigurationProperty("isBidirectional", DefaultValue = false)]
       public bool IsBidirectional
       {
           get
           {
               return (bool)this["isBidirectional"];
           }
           set
           {
               this["isBidirectional"] = value;
           }
       }

        public override Type BehaviorType
        {
            get { return typeof(DispatchMessageInspectorTestBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new DispatchMessageInspectorTestBehavior(IsBidirectional);
        }
    }
}

 

test
相关文章
|
9月前
|
网络协议 网络架构 Windows
框架学习——WCF框架
框架学习——WCF框架
231 0
|
安全 C#
WCF技术我们应该如何以正确的方式去学习掌握
一、WCF技术我该如何学习?       阿笨的回答是:作为初学者的我们,那么请跟着阿笨一起玩WCF吧,阿笨将带领大家如何以正确的姿势去掌握WCF技术。由于WCF技术知识点太多了,就纯基础概念性知识都可以单独出一本书来讲解,本次分享课程《C#面向服务编程技术WCF从入门到实战演练》开课之前,阿笨还是希望从没了解过WCF技术的童鞋们提前先了解一下WCF技术,至少要明白WCF技术的ABC三要素分别指的是什么。
1172 0
|
前端开发 .NET 开发框架
Wcf扩展
ASP.NET MVC和WCF真是微软两个很棒的框架,设计的很好,可扩展性非常强,到处都是横切、管道。 以前写过一篇MVC流程的文章,http://www.cnblogs.com/lovecindywang/archive/2010/12/02/1894740.html主要是使用了MVC的各种扩展。
785 0
跟着Artech学习WCF扩展(1) Binding进行通信
这个demo简单 就一个服务器段的一个客户端的 主要是注释 Server的 using System; using System.Collections.Generic; using System.
690 0
|
机器学习/深度学习 消息中间件 开发框架
跟着Artech学习WCF(4) MSMQ 绑定
曾几何时 再看大家讨论各种构架时,经常出现在各个服务器间传递数据,经常出现MSMQ, 那是看到这个心理就郁闷,怎么整天折腾ASP.NET 就没遇见过这个东西 原来这个家伙藏在WCF里面 嘿嘿这次被我发现了 首先 第一次装IIS的话 默认是没有安装msmq 所以需要自己安装的   看Art...
784 0
|
XML 网络架构 数据格式
跟着Artech学习WCF(5) Responsive Service 与自定义Soap Message Header
记得以前看.NET各类框架教程在介绍SOAP时经常提到Soap Header 以前一直认为这个玩意就是个理论 应该和具体的编码和应用无关 后来在看到一些关于SOAP安全的书可以在header里 进行加密解密信息的存储 用于安全方面的验证 但一直苦于这个玩意到底是神马东西,一直没见过代码,今天A...
683 0
|
存储
跟着Artech学习WCF(3) wcf 的状态问题
开始以为是wcf的session问题 敲了一边代码发现里面没有用session存储数据 经过 自己研究才发现作者是再将wcf的状态存储问题 项目结构如下   代码如下 using System; using System.
687 0