none
WCF Message Inspector RRS feed

  • Question

  • Hi,

    I am writing a WCF Message Inspector by implementing IDispatchMessageInspector.   Within the message inspector, I am planning to write code that sends data to a service bus queue.   Connecting to the Service Bus Queue generally takes more time.   Now, my question is,

    1. Do we create a new instance of Message Inspector every time a request is received?.

    Thanks,

    Ravi.


    Ravi

    Friday, August 15, 2014 5:52 PM

All replies

  • Hi,

    The inspector interfaces have two methods: one after the incoming message is received on the wire and before it’s dispatched to the application (AfterReceive), and one after the outgoing message is received from the application and before it’s encoded to be sent on the wire (BeforeSend).

    At the server side, implementations of IDispatchMessageInspector get a chance to inspect the incoming message right after the request is received via the AfterReceiveRequest method. Besides the message, the method is also passed the channel through which the message arrived (which contains information about any current sessions, local and remote address, and so on), and the instance context associated with the service (with references to the host, and any extensions associated with it). After the user code on the service operation processed the request, and the reply is created into a Message object, BeforeSendReply is called on the inspector code, with the message object and the correlation state, which is whatever AfterReceiveRequest returned – this way the code can correlate the two parts of the message inspection for a single client request.

    One thing that deserves mention is that the Message object to both methods is passed by reference. WCF Message objects can only be “consumed” once – and “consumed” can mean read, written or copied. The message body is essentially a read-once stream, so once it’s consumed it cannot be used again. So if, in the inspector code, one were to read the message, the WCF runtime wouldn’t be able to reuse that message in the rest of its pipeline (i.e., to encode it to send as a reply or to parse it into operation parameters). So if the inspector code needs to read the message, it’s the responsibility of the inspector to recreate the message.

    For detailed information, you could refer to:

    http://blogs.msdn.com/b/endpoint/archive/2011/04/23/wcf-extensibility-message-inspectors.aspx

    http://weblogs.asp.net/paolopia/writing-a-wcf-message-inspector

    Regards

    Monday, August 18, 2014 5:56 AM
    Moderator
  • Hi Pactera,

    Thanks for the response.  What I am looking is, does the runtime creates a new instance of the message inspector everytime it receives a request?.  If it does, then the performance takes a hit.  Because, my ServiceBus connection takes more time, which I want to avoid.  

    My guess is, runtime creates a new instance of the message inspector everytime.  I am just trying to verify with the experts.

    Thanks


    Ravi

    Monday, August 18, 2014 11:48 AM