OperationContextScope vs MessageInpectors

940 Views Asked by At

Help me in understanding differences between these two. as per to me, Operation ContextScope can be used irrespective of .NET application you are using like WCF, Console, Web, etc, anywhere this can be used if you are calling any other service like WCF or Java based service [ it this will not work in case of ASMX service] to add headers into outgoing message.

If so then why we need MessageInspectors at any client side to add headers? OperationContextScope is much simpler than MessageInspectors. anybody shed some light to understand the correct usage of these two?

1

There are 1 best solutions below

1
On BEST ANSWER

IClientMessageInspector on the client side and IDispatchMessageInspector, on the server side are good at inspecting the body of messages, potentially modifying the message before sending, or modifying what is received.

Here is a sample :

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;
    }
}

Write a behavior to easily apply the inspector to a client :

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
}

Use the behavior :

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

        cf.Endpoint.Behaviors.Add(
            new MyInspectorBehavior()
            );

Same thing could be done on the server side with IDispatcherMessageInspector.
The behavior could be put with C#, XML (app.config/web.config) or declaratively on the service implementation :

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

OperationContextScope are useful for handling headers (adding, removing).

Programming WCF Services, Appendix B, from Juval Löwy explains pretty well OperationContextScope. Juval's Framework, ServiceModelEx helps using OperationContextScopes with the GenericContext<T> class

See Juval's Company site for download : http://www.idesign.net/Downloads

Regards