C# SOAP WCF:防止反序列化到私有字段
我试图针对WCF执行来自SoapUI的SOAP调用,当XML被反序列化时,它试图反序列化到私有字段。为什么会发生这种情况?我该如何应对?代码如下所示: 我使用标准XSD.exe从XSD文件生成了POCO类,结果如下所示:C# SOAP WCF:防止反序列化到私有字段,c#,xml,wcf,soap,C#,Xml,Wcf,Soap,我试图针对WCF执行来自SoapUI的SOAP调用,当XML被反序列化时,它试图反序列化到私有字段。为什么会发生这种情况?我该如何应对?代码如下所示: 我使用标准XSD.exe从XSD文件生成了POCO类,结果如下所示: /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")] [System.SerializableAttribute()] [System.Diagnosti
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://iec.ch/TC57/2011/MeterConfig#")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://iec.ch/TC57/2011/MeterConfig#", IsNullable = false)]
public partial class MeterConfig
{
private ComFunction[] comFunctionField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ComFunction")]
public ComFunction[] ComFunction
{
get { return this.comFunctionField; }
set { this.comFunctionField = value; }
}
}
我有一个SoapUI测试项目,其中提供了以下XML:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:CreateMeterConfig2>
<tem:Payload>
</tem:Payload>
</tem:CreateMeterConfig2>
</soapenv:Body>
</soapenv:Envelope>
或全部:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</faultcode>
<faultstring xml:lang="en-ZA">The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:Payload. The InnerException message was 'Error in line 13 position 24. 'EndElement' 'Payload' from namespace 'http://tempuri.org/' is not expected. Expecting element 'comFunctionField'.'. Please see InnerException for more details.</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
答:反序列化失败
格式化程序在尝试反序列化消息时引发异常:尝试反序列化参数时出错http://tempuri.org/:Payload. InnerException消息是“第13行位置24处出错”命名空间中的EndElement“”负载'http://tempuri.org/”他说。应为元素“comFunctionField”。有关更多详细信息,请参阅InnerException。
您的问题是,您自动生成了一个标记为的类型,但使用的是WCF。当尝试序列化标记为但不为的类型时,它推断出一个类似于BinaryFormatter
工作方式的约定,即应序列化公共和私有字段,而不是属性。(有关更多信息,请参阅。)
要解决此问题,您可以:
XmlSerializer
。为此,请参见和,例如:
svcutil.exe
而不是XSD.exe
,使用数据协定属性而不是XmlSerializer
属性从XSD自动生成类。要执行此操作,请参见和
请注意,DataContractSerializer
与XmlSerializer
相比有一些限制。例如,它不允许将属性选择性地序列化为XML属性而不是元素。有关详细信息,请参阅或。如果XSD与这些限制不兼容,则需要使用XmlSerializer
非常彻底的回答,谢谢。我加入了[XmlSerializerFormatAttribute],解决了这个问题。
Expecting element 'comFunctionField'
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</faultcode>
<faultstring xml:lang="en-ZA">The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:Payload. The InnerException message was 'Error in line 13 position 24. 'EndElement' 'Payload' from namespace 'http://tempuri.org/' is not expected. Expecting element 'comFunctionField'.'. Please see InnerException for more details.</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
[ServiceContract]
[XmlSerializerFormat]
public class MyApi
{
[OperationContract]
public void CreateMeterConfig2(MeterConfig Payload)
{
//do nothing
}
}