本文介绍了如何在 MessageContract 类型中使用 XML 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预先说明:我无法更改传入的 SOAP 请求的格式,因为它们已由国际标准 (weeeeeeeee) 固定.

A note up front: I cannot change the format of the SOAP requests coming in, as they are fixed by international standards (weeeeeeeee).

我有一个 SOAP 请求进入我的 WCF 服务,看起来像这样:

I have a SOAP request that comes into my WCF service looking something like this:

<s:Body>
    <Request version="1.0">
        <data someOtherVersion="1.1">
            ...
        </data>
    </Request>
</s:Body>

到目前为止,我们一直在直接使用 System.ServiceModel.Channels.Message 对象,这有点痛苦.我们正在尝试转向使用如下所示的强类型:

Up to now, we've been using System.ServiceModel.Channels.Message objects directly, which is kind of a pain. We're trying to move to using strong types that look like this:

[MessageContract(IsWrapped = false)]
public class Request
{
    [MessageBodyMember]
    [XmlAttribute("version")]
    public string Version;

    [MessageBodyMember]
    [XmlElement("data")]
    public SomeOtherType Data;
}

[MessageContract(IsWrapped = false)]
public class Response
{
    [MessageBodyMember]
    [XmlAttribute("version")]
    public string Version;

    [MessageBodyMember]
    [XmlElement("data")]
    public SomeOtherType ResponseData;
}

[ServiceContract]
[XmlSerializerFormat]
public interface Service
{
    [OperationContract(Action = "request", ReplyAction = "response")]
    Response ServiceOperation(Request req);
}

不幸的是,当我们尝试启动时,我们收到错误消息System.ServiceModel.dll 中发生了类型为‘System.InvalidOperationException’的未处理异常

Unfortunately, when we try to start up, we get an error saying "An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll

附加信息:XmlSerializer 属性 System.Xml.Serialization.XmlAttributeAttribute 在版本中无效.当 IsWrapped 为 false 时,MessageContract 中仅支持 XmlElement、XmlArray、XmlArrayItem 和 XmlAnyElement 属性."

Additional information: XmlSerializer attribute System.Xml.Serialization.XmlAttributeAttribute is not valid in Version. Only XmlElement, XmlArray, XmlArrayItem and XmlAnyElement attributes are supported in MessageContract when IsWrapped is false."

有趣的是,将IsWrapped"设置为 true 会产生相同的错误.有没有办法在消息协定类型中序列化 XML 属性,或者在这里使用包装器是我们唯一的选择?

Interestingly, setting "IsWrapped" to true gives the same error. Is there a way to serialize XML attributes in message contract types, or is using wrappers our only option here?

推荐答案

不幸的是,我发现实现这一目标的唯一方法是使用包装类

Unfortunately the only way I found to achieve this is to use a wrapper class

[MessageContract(IsWrapped = false)]
public class Response
{
    [MessageBodyMember(Name = "Response", Namespace = "Http://example.org/ns1")]
    public ResponseBody Body { get; set; }

    public Response(){}


    public Response(ResponseBody body)
    {
        Body = body;
    }
}

[XmlType(AnonymousType = true, Namespace = "Http://example.org/ns1")]
public class ResponseBody
{
    [XmlAttribute(AttributeName = "version")]
    public string Version { get; set; }

    [XmlElement(ElementName = "data", Namespace = "Http://example.org/ns1")]
    [MessageBodyMember]
    public SomeOtherType ResponseData { get; set; }
}

这篇关于如何在 MessageContract 类型中使用 XML 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:10