如何从SOAP响应XML创建WCF消息对象

时间:2013-06-05 21:03:11

标签: c# .net wcf soap

为了对一个用于处理WCF响应的类进行单元测试,我使用Message.ToString()获取了一个实际的Message Response,这样我就不必实际进行WCF调用(从而使它成为集成测试,而不是单元测试)。如何基于来自Message.ToString()输出的SOAP XML重新构造Message对象,或者甚至可能?我不知道哪个Message.CreateMessage()重载解决了这种情况。

这是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+t4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UZ+WkfPnvxTZRFlBcavYax2hgPlRHOjM6E4NuxQPgiwElyI8kAPG/V1OKCss9KE4RN9B+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>

那么,我该如何构建一个消息?

2 个答案:

答案 0 :(得分:7)

如果您拥有邮件的整个信封,则可以使用重载Message.CreateMessage(XmlDictionaryReader, int, MessageVersion)创建Message对象。使用与此类似的代码在邮件内容上创建XmlDictionaryReader

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

现在,您拥有的邮件有一些时间戳,已加密并签名。一旦你将它传递给WCF,它可能会无效......

答案 1 :(得分:0)

您可以在以下链接中找到有关从XML创建XSD,然后从XSD创建新类的说明。

Generate C# class from XML

创建此对象的实例,并将其序列化以生成新的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);