【问题标题】:OperationContextScope vs MessageInpectorsOperationContextScope 与 MessageInpectors
【发布时间】:2015-12-25 04:58:10
【问题描述】:

帮助我了解这两者之间的差异。据我所知,操作 ContextScope 可以用于任何您使用的 .NET 应用程序,如 WCF、控制台、Web 等,如果您正在调用任何其他服务(如 WCF 或基于 Java 的服务),则可以在任何地方使用它[这在 ASMX 服务的情况下不起作用] 将标头添加到传出消息中。

如果是这样,为什么我们需要在任何客户端使用 MessageInspectors 来添加标头? OperationContextScope 比 MessageInspector 简单得多。有没有人了解一下这两个的正确用法?

【问题讨论】:

    标签: wcf soapheader idispatchmessageinspector operationcontext iclientmessageinspector


    【解决方案1】:

    客户端的IClientMessageInspector 和服务器端的IDispatchMessageInspector 擅长检查消息体,可能会在发送前修改消息,或修改接收到的内容。

    这是一个示例:

    public class MyMessageInspector : IClientMessageInspector
    {
        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        { 
            // Inspect and/or modify the message here
            MessageBuffer mb = reply.CreateBufferedCopy(int.MaxValue);
            Message newMsg = mb.CreateMessage();
    
            var reader = newMsg.GetReaderAtBodyContents().ReadSubtree();
            XElement bodyElm = XElement.Load(reader);
            // ...
            reply = newMsg;
        }
        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            // Something could be done here
            return null;
        }
    }
    

    编写一个行为以轻松地将检查器应用于客户端:

    public class MyInspectorBehavior : IEndpointBehavior
    {
        #region IEndpointBehavior Members
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(
                new MyMessageInspector()
                );
        }
    
        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {}
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {}
        public void Validate(ServiceEndpoint endpoint)
        {}
        #endregion
    }
    

    使用行为:

            ChannelFactory<IService1> cf = 
                new ChannelFactory<IService1>(
                    new BasicHttpBinding(),
                    "http://localhost:8734/DataService.svc");
    
            cf.Endpoint.Behaviors.Add(
                new MyInspectorBehavior()
                );
    

    使用 IDispatcherMessageInspector 可以在服务器端完成相同的操作。
    该行为可以使用 C#、XML (app.config/web.config) 或以声明方式放在服务实现中:

    [MyServiceInspectorBehavior]
    public class ServiceImpl : IService1
    { ...}
    

    OperationContextScope 对于处理标题(添加、删除)很有用。

    Juval Löwy 的《Programming WCF Services》附录 B 很好地解释了 OperationContextScope。 Juval 的框架,ServiceModelEx 帮助使用 OperationContextScopesGenericContext&lt;T&gt;

    查看 Juval 的公司网站进行下载:http://www.idesign.net/Downloads

    问候

    【讨论】:

    • 感谢伊曼纽尔的回复。我知道 MessageInspectors 纯粹是基于 WCF 的,OperationContextScope 怎么样..它可以在 WCF 之外使用,比如控制台应用程序等。例如,我的控制台应用程序正在调用基于 java 的 Web 服务,它需要一个标头,我可以使用operationcontextscope 在发送请求时向其中添加标头?
    猜你喜欢
    • 2012-03-18
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多