预先注意:我无法更改进入的SOAP请求的格式,因为它们是由国际标准(weeeeeeeee)修复的。
我有一个SOAP请求进入我的WCF服务,看起来像这样:
<s:Body>
<Request version="1.0">
<data someOtherVersion="1.1">
...
</data>
</Request>
</s:Body>
到目前为止,我们一直在使用System.ServiceModel.Channels.Message
个对象,这有点痛苦。我们正试图使用看起来像这样的强类型:
[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'类型的未处理异常
其他信息:XmlSerializer属性System.Xml.Serialization.XmlAttributeAttribute在版本中无效。当IsWrapped为false时,MessageContract中仅支持XmlElement,XmlArray,XmlArrayItem和XmlAnyElement属性。“
有趣的是,将“IsWrapped”设置为true会产生相同的错误。有没有办法在消息协定类型中序列化XML属性,或者使用包装器是我们唯一的选择吗?
答案 0 :(得分:1)
不幸的是,我发现实现此目标的唯一方法是使用包装器类
[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; }
}
答案 1 :(得分:-3)
尝试使用XmlElement(typeof(data))