3

In order to unit test a class for handling WCF responses, I snagged an actual Message Response using Message.ToString() so that I don't have to actually make the WCF call (thus making it an integration test rather than a unit test). How do I re-construct a Message object based on that SOAP XML that comes from the Message.ToString() output, or is it even possible? I don't know which Message.CreateMessage() overloads addresses this situation.

Here's the result of the Message.ToString():

    <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
      <s:Header>
        <a:Action s:mustUnderstand="1" u:Id="_4">http://schemas.xmlsoap.org/ws/2004/09/enumeration/EnumerateResponse</a:Action>
        <a:RelatesTo u:Id="_5">urn:uuid:3c4e24a9-af47-4f4a-879a-04a5e0296cd5</a:RelatesTo>
        <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
          <u:Timestamp u:Id="uuid-73a747a6-ffda-43f2-908d-4e1d9ad52eed-1810">
            <u:Created>2013-06-05T17:11:39.734Z</u:Created>
            <u:Expires>2013-06-05T17:16:39.734Z</u:Expires>
          </u:Timestamp>
          <c:DerivedKeyToken u:Id="_0" xmlns:c="http://schemas.xmlsoap.org/ws/2005/02/sc">
            <o:SecurityTokenReference>
              <o:Reference URI="urn:uuid:368c80e4-4044-4b00-8da4-8ba6c9f7c8d6" ValueType="http://schemas.xmlsoap.org/ws/2005/02/sc/sct" />
            </o:SecurityTokenReference>
            <c:Offset>0</c:Offset>
            <c:Length>24</c:Length>
            <c:Nonce>lnBkV4BtA6KOTkUGykrSyw==</c:Nonce>
          </c:DerivedKeyToken>
          <c:DerivedKeyToken u:Id="_1" xmlns:c="http://schemas.xmlsoap.org/ws/2005/02/sc">
            <o:SecurityTokenReference>
              <o:Reference URI="urn:uuid:368c80e4-4044-4b00-8da4-8ba6c9f7c8d6" ValueType="http://schemas.xmlsoap.org/ws/2005/02/sc/sct" />
            </o:SecurityTokenReference>
            <c:Nonce>oA/QSxlAEfZnE4Z3OgPX+w==</c:Nonce>
          </c:DerivedKeyToken>
          <e:ReferenceList xmlns:e="http://www.w3.org/2001/04/xmlenc#">
            <e:DataReference URI="#_3" />
            <e:DataReference URI="#_6" />
          </e:ReferenceList>
          <e:EncryptedData Id="_6" Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns:e="http://www.w3.org/2001/04/xmlenc#">
            <e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" />
            <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
              <o:SecurityTokenReference>
                <o:Reference URI="#_1" />
              </o:SecurityTokenReference>
            </KeyInfo>
            <e:CipherData>
              <e:CipherValue>kLxDgVRs8jVM6/e7BF5E4886rW9sN55/6d6GpLO9UiOQU48eDWZ1JDTwYBna+tddekUWIeNf47E8KqPg87NYauD/JxspZqhS6Ifnxr2GlP2xx05ttpQ1hDfLIsqU5YX1fxMD9aAVKlv3nA73NnpRO3HbEN8sXPJTf2hEyaj6qxh6mbST7j2CsBdv7qXXiq8y2s/QMOH+7TnVDvgdT8FCFI62S2U6yqv67QnUjIoSe0QXuR3x4dXmmw435jAvukeJ+elk3845SSZUITSh6JOp3jjkqwarnWrqv4Nz30SU2IoczXhuq/Ln4TF/SFoqCEW7+WR+gnYOAs99NvrsWsrVa2Fcc0/qVzWNqXRDRQRiTnQEhnN7kKLO9Fodb8dG1+DkVbEJJ+zSS0WuFwKQnSfiWGXBEFR9y3moLNfPnMzHxYwnxAbdumCgYA7+iZGi87bIj/P1OzHv3NMR/lts8ugYOOKYRRHnImSTwBqzurQDYO+PDS5zBIDEBDXqt1AvazvtxnnpL7i/Shhze8gZMMcH2+xUJ78Ivv8UphusINOHRg3gAxSG+6zsxfflCnuku528oGMMcAHzeSFGKRgD+FkB0Rbii9jGzYPtv8C8Kv+t4DKgmfq5OfMowgqqcVBkcLz4aafKrcshtB0+vJdeJEbv6lORDABgxVbM73Q5vUbSflcw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RN9B+MQhTGbv0t6SSpDb5eJIM6DbPcXZDUFnVGO03zKY3gEr6MWS0TUSy3/uTHsAUgqGwrKIUmtbgUmLBBJjMA+5kF2zAfykkISuswGAuUsbiQXSmDnul1VkbrXxyYXV5aRjWrArHAmsVE2EmfBhP5V9Y65czRuFUEsQZQRBXgy50eDJ2dB9Rp27xON8VBNYAwlMvMIbbcKA2a49QzNvXKTfpnmhd5uPPZkSwKtZWx29UH6eJbzpztKTrLjEgbPtl1w8XyHDuWQwXNeRKCc2VB96I595E</e:CipherValue>
            </e:CipherData>
          </e:EncryptedData>
        </o:Security>
      </s:Header>
      <s:Body u:Id="_2">
        <EnumerateResponse xmlns="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <EnumerationContext>
            <CurrentIndex xmlns="http://schemas.microsoft.com/2006/11/ResourceManagement">0</CurrentIndex>
            <EnumerationDirection xmlns="http://schemas.microsoft.com/2006/11/ResourceManagement">Forwards</EnumerationDirection>
            <Expires xmlns="http://schemas.microsoft.com/2006/11/ResourceManagement">9999-12-31T23:59:59.9999999</Expires>
            <Filter xmlns="http://schemas.microsoft.com/2006/11/ResourceManagement">/Person</Filter>
            <LocalePreferences xsi:nil="true" xmlns="http://schemas.microsoft.com/2006/11/ResourceManagement" />
            <Selection xmlns="http://schemas.microsoft.com/2006/11/ResourceManagement">
              <string>ObjectID</string>
            </Selection>
            <Sorting xsi:nil="true" xmlns="http://schemas.microsoft.com/2006/11/ResourceManagement" />
          </EnumerationContext>
          <EnumerationDetail xmlns="http://schemas.microsoft.com/2006/11/ResourceManagement">
            <Count>926</Count>
          </EnumerationDetail>
          <Expires>12/31/9999 23:59:59</Expires>
        </EnumerateResponse>
      </s:Body>
    </s:Envelope>

So, how do I construct a Message out of that?

2
  • have you tried this: stackoverflow.com/questions/6131111/… Commented Jun 5, 2013 at 21:16
  • another route is to use xsd tool to build a class that captures the data, then deserialize, but this pretty messy. Do you not have access to the WSDL? 'cause that would be way simpler. Commented Jun 5, 2013 at 21:33

2 Answers 2

8

If you have the whole envelope of a message, you can create the Message object by using the overload Message.CreateMessage(XmlDictionaryReader, int, MessageVersion). Create a XmlDictionaryReader over the contents of the message by using a code similar to this one:

var bytes = Encoding.UTF8.GetBytes(theMessageString);
var reader = XmlDictionaryReader.CreateTextReader(bytes, XmlDictionaryReaderQuotas.Max);

Now, the message you have has some timestamps, is encrypted and signed. It's possible that once you pass it to WCF, it will be invalid...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Carlos - that did the job. Yes, there are timestamps, signatures, etc., but what I really needed to test was the handling of the body, which your example code got me to just fine. I'm not passing it to WCF, just unit testing.
0

You'll find instructions at the link below for creating an XSD from the XML, then creating a new class from the XSD.

Generate C# class from XML

Create an instance of this object, and serialize it to generate the new XML:

public class SoapThing
{
...
}


SoapThing p=new SoapThing ();
      p.x = some values;
      System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
      x.Serialize(Console.Out, p);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.