Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SOAP WCF:防止反序列化到私有字段_C#_Xml_Wcf_Soap - Fatal编程技术网

C# SOAP WCF:防止反序列化到私有字段

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

我试图针对WCF执行来自SoapUI的SOAP调用,当XML被反序列化时,它试图反序列化到私有字段。为什么会发生这种情况?我该如何应对?代码如下所示:

我使用标准XSD.exe从XSD文件生成了POCO类,结果如下所示:

/// <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
工作方式的约定,即应序列化公共和私有字段,而不是属性。(有关更多信息,请参阅。)

要解决此问题,您可以:

  • 通过应用于您的服务,将WCF配置为使用
    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
        }
    }