wcfidispatchmessageinspector

Explain read a message object once with WCF?


I have a MessageInspector which logs the messages that come through? What is the reason why you can only read a Message once and must create a copy? I have seen the documentation from MSDN that I need to create a buffered copy, but I don't know why it is implemented this way? Can someone explain it to me?

private static void SendRequest(string request)
{
var req = (HttpWebRequest) WebRequest.Create("http://urltoservice.svc/MethodToCall");
req.ContentType = "text/xml";
req.Method = "POST";

using (var stm = req.GetRequestStream())
{
    using (var stmw = new StreamWriter(stm))
    {

        stmw.Write(request);
    }
}


byte[] myData;
using (var webResponse = req.GetResponse())
{

    var responseStream = webResponse.GetResponseStream();
    myData = ReadFully(responseStream);
}

// Do whatever you need with the response
string responseString = Encoding.ASCII.GetString(myData);
}

If I don't have access to the server part or the ability to change the MessageInspector to use a buffered copy of the message, can I modify the message above to make a copy of a stream? If so, how would I go about doing that?


Solution

  • You can copy your message into a buffer and play with it. More details about working with messages you can find on the following link: http://msdn.microsoft.com/en-us/library/ms734675.aspx