如何将WCF SOAP消息当选为生成类

发布于 2025-01-28 03:38:21 字数 1979 浏览 1 评论 0原文

SOAP响应消息:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:stef">
   <soap:Body>
      <tns:ConvertResponse>
         <tns:Result>abc</tns:Result>
      </tns:ConvertResponse>
   </soap:Body>
</soap:Envelope>

生成的代码,该代码在添加Service-Reference( sippet )时生成的代码:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName = "ConvertResponse", WrapperNamespace = "urn:stef", IsWrapped = true)]
public partial class ConvertResponse
{

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace = "urn:stef", Order = 0)]
    public string Result;

    public ConvertResponse()
    {
    }

    public ConvertResponse(string Result)
    {
        this.Result = Result;
    }
}

我使用以下通用代码将此SOAP字符串对Convertresponse进行了启用。对象:

private static T Deserialize<T>(string xml)
{
    var stream = new MemoryStream(Encoding.ASCII.GetBytes(xml));
    var message = Message.CreateMessage(XmlReader.Create(stream), int.MaxValue, MessageVersion.Soap11);
    var importer = new SoapReflectionImporter(new SoapAttributeOverrides(), "urn:stef");
    var mapping = importer.ImportTypeMapping(typeof(T));
    var xmlSerializer = new XmlSerializer(mapping);

    return (T)xmlSerializer.Deserialize(message.GetReaderAtBodyContents());
}

解决方案基于 https://stackoverflow.com/a/a/43901543/17218587

上面的 因为contrestresponse类没有 public result的属性,所以这就是原因result在求职后无效。

所以我的问题是,是否有一种方法可以将此XML肥皂消息划分到(生成的)类中?

Soap Response message:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:stef">
   <soap:Body>
      <tns:ConvertResponse>
         <tns:Result>abc</tns:Result>
      </tns:ConvertResponse>
   </soap:Body>
</soap:Envelope>

Generated code, which is generated by VS when adding a service-reference (snippet) :

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName = "ConvertResponse", WrapperNamespace = "urn:stef", IsWrapped = true)]
public partial class ConvertResponse
{

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace = "urn:stef", Order = 0)]
    public string Result;

    public ConvertResponse()
    {
    }

    public ConvertResponse(string Result)
    {
        this.Result = Result;
    }
}

I used the following generic code to deserialize this soap string into the ConvertResponse object:

private static T Deserialize<T>(string xml)
{
    var stream = new MemoryStream(Encoding.ASCII.GetBytes(xml));
    var message = Message.CreateMessage(XmlReader.Create(stream), int.MaxValue, MessageVersion.Soap11);
    var importer = new SoapReflectionImporter(new SoapAttributeOverrides(), "urn:stef");
    var mapping = importer.ImportTypeMapping(typeof(T));
    var xmlSerializer = new XmlSerializer(mapping);

    return (T)xmlSerializer.Deserialize(message.GetReaderAtBodyContents());
}

The solution above is based on https://stackoverflow.com/a/43901543/17218587

But this does not work because the ConvertResponse class does not have a public property for Result, so that's the reason Result is null after deserialization.

So my question is if there is a way to deserialize this xml-soap message into that (generated) class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再见 2025-02-04 03:38:21

我不知道Stef Heyenrath是否得到了答案,但是这里的问题是,SoaprefreflectionImporter和XMLSerializer旨在与用XMLSerialializerFormat标记的消息一起使用。但是,由于它具有MessageCecntract,因此我们也不能使用DataContractSerialializer。

I also needed to deserialize messages, and after much fiddling in google, found the answer here:

private static T Deserialize<T>(string xml, bool useXml)
{
    var stream = new MemoryStream(Encoding.ASCII.GetBytes(xml));
    var message = Message.CreateMessage(XmlReader.Create(stream), int.MaxValue, MessageVersion.Soap11);

    TypedMessageConverter converter = null;

    if (useXml)
        converter = TypedMessageConverter.Create(typeof(T), "*", string.Empty, new XmlSerializerFormatAttribute());
    else
        converter = TypedMessageConverter.Create(typeof(T), "*", string.Empty, new DataContractFormatAttribute());

    return (T)converter.FromMessage(message);
}

I don't know if Stef Heyenrath got the answer to this, but the question here is, SoapReflectionImporter and XmlSerializer are meant to be used with messages marked with XmlSerializerFormat. But, since it has MessageContract, we cannot use DataContractSerializer either.

I also needed to deserialize messages, and after much fiddling in google, found the answer here: WCF DataContractSerializer doesn't pick up contract attributes... why not?

So, according to blowdart, your code would be something like this:

private static T Deserialize<T>(string xml, bool useXml)
{
    var stream = new MemoryStream(Encoding.ASCII.GetBytes(xml));
    var message = Message.CreateMessage(XmlReader.Create(stream), int.MaxValue, MessageVersion.Soap11);

    TypedMessageConverter converter = null;

    if (useXml)
        converter = TypedMessageConverter.Create(typeof(T), "*", string.Empty, new XmlSerializerFormatAttribute());
    else
        converter = TypedMessageConverter.Create(typeof(T), "*", string.Empty, new DataContractFormatAttribute());

    return (T)converter.FromMessage(message);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文