简体   繁体   中英

WCF Inspect Messages

I'm trying to implement a simple message inspector that writes the message to the debug window from an example on MSDN :

public class MyMessageInspector : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        System.Diagnostics.Debug.WriteLine(request.ToString());
        return null;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        System.Diagnostics.Debug.WriteLine(reply.ToString());
    }
}

The reply is writing as expected. However the request seems to be null. Any ideas on what could be going wrong? I'm using a Service Reference proxy with a console app as the client.

I'm using basicHttpbinding and hosting with IIS with svc file. The parameter for my web method is a complex type. I'm not sure if that makes a difference.

Try CreateBufferedCopy (ie clone) of the message request first: http://msdn.microsoft.com/en-us/library/ms734675.aspx (Copying a Message into a Buffer).

More info here under "Now for the message inspection part": http://binarymist.net/2010/06/14/message-inspection-in-wcf/

I have copy and pasted the MyMessageInspector class and added the behavior to my my web service, and it works fine - when the web service is called, the SOAP envelope is printed as XML.

Do you have any other MessageInspectors in your project? If so, it is possible that one of them is setting request = null - this will cause the problem you're having, since the request parameter is ref .

If not, what makes you say request is null? Are you getting a NullReferenceException on the Debug.WriteLine(..) statement?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM