Can not restore signed SOAP message from XML to System.ServiceModel.Channels.Message RRS feed

  • Question

  • Title: "Can not restore signed SOAP message from XML to System.ServiceModel.Channels.Message"

    Goal: "Needs to override signed SOAP response in client message interceptor (mock implementation)."

    Preconditions: ".Net 4.5.1, Windows 7 Professional SP1, VS 2012 Premium Update 4, Gold MS Partner"

    //Code (note - the soap response should be signed):

    public class SoapMessageIntereceptor : IClientMessageInspector

      //Main method, which reproduces a problem

      private Message CopyMessage(Message message)
                //Create copy of the message and convert to XML
                MessageBuffer buffer = message.CreateBufferedCopy(int.MaxValue);
                Message copy = buffer.CreateMessage();
                XmlDocument doc = copy.ToXml();

                //Try to restore the same message from XML
                XmlReader xmlReader = new XmlNodeReader(doc);
                Message newMessage = Message.CreateMessage(xmlReader, int.MaxValue, message.Version);

                //Compare original and restore message headers ("wsse:Security" header)
                MessageHeaderInfo originalHeader = message.Headers[0];
                MessageHeaderInfo newHeader = newMessage.Headers[0];

                string originalHeaderXml = originalHeader.ToString();
                string newHeaderXml = newHeader.ToString();
                if (originalHeaderXml != newHeaderXml)
                    throw new InvalidOperationException("Original and restore headers are not equals");

                return newMessage;


      public override void OnSend(ref Message request)

      public void AfterReceiveReply(ref Message reply, object correlationState)

                reply = CopyMessage(reply);

    Allegedproblem: the restore from XML SOAP message header has wrong XML structure for "<SignedInfo>" section.
    Original XML header has following "empty" tags structure:
          <CanonicalizationMethod Algorithm=""></CanonicalizationMethod>
          <SignatureMethod Algorithm=""></SignatureMethod>

    But "restored" XML header has  following strcucture:
          <CanonicalizationMethod Algorithm="">
          <SignatureMethod Algorithm="">

    It looks like there is some problem in "XmlBinaryReader" or "XmlBinaryWriter", but unfortunately they are internal.

    Is it possible to restore the signed SOAP message from XML?


    MS Certified Professional Developer ----------------------------------- Arcadia, ZAO Postal address: P.O.B 8, 195112, St.Petersburg, Russia Visiting address: Zanevskiy pr. 30, korp. 2A, 195112, St.Petersburg, Russia Reception: +7 (812) 610 59 55, 610 59 58 Fax: +7 (812) 610 59 57

    Tuesday, July 8, 2014 9:11 AM

All replies

  • Now we need to intercept the XML that gets created during SoapHttpClientProtocol.Invoke().  There’s a convenient point for doing that: GetWriterForMessage().  It’s responsible for returning an XmlWriter that gets used to build the SOAP XML message.

    To do that, we’ll need our own XmlWriter that wraps another XmlWriter, the original one returned by the HelloService class.  Our strategy is to intercept all calls to the original XmlWriter and write those to our StringWriter.  It’s an XmlWriterSpy.  Here’s how it looks (some methods omitted for brevity): .

    What do you mean about the "Restore the signed soap message from XML"?

    Wednesday, July 9, 2014 6:36 AM