gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:12:37 25 4
gpt4 key购买 nike

为了对处理 WCF 响应的类进行单元测试,我使用 Message.ToString() 获取了一个实际的消息响应,这样我就不必实际进行 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+FkB0Rbii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+mONyH+d417Ag+/C45RJUZ+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>

那么,我如何从中构建消息?

最佳答案

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

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

现在,您的消息有一些时间戳,已加密并签名。有可能一旦传给WCF,就失效了...

关于c# - 如何从 SOAP 响应 XML 创建 WCF 消息对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16949843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com