即使我使用XmlSerializer,也会使用字段后缀创建WCF自动生成的类

时间:2019-04-02 14:10:23

标签: c# xml wcf soap xmlserializer

我有一个核心服务和一个微服务。微服务引用核心WCF服务。当我添加引用时,references.cs包含一些类,这些类的私有属性公开为公共,并且我所有的属性都带有字段后缀

例如:

public string SMCD { get; set; } 变成 private string sMCDField;

以下是我引用的课程:

CoreInterface.cs

[ServiceContract(Namespace = Constants.Namespace, Name = "M3ApiCalls")]
public interface IService
{

    [System.ServiceModel.XmlSerializerFormatAttribute()]
    [OperationContractAttribute(AsyncPattern = true)]
    IAsyncResult BeginCRS100MI_List(string Salesperson, decimal Timestamp, AsyncCallback asyncCallback, object state);
    [System.ServiceModel.XmlSerializerFormatAttribute()]
    CRS100MI_ListResult EndCRS100MI_List(IAsyncResult result);

    ...(more operations)
}

CRS100MI_ListResult.cs

[Serializable()]
[XmlSerializerFormat()]
[XmlRoot(ElementName = "CRS100MI_List",DataType = "System.Xml.XmlElement",Namespace = "http://companynamespace")]
public class CRS100MI_ListResult
{
    [XmlElement(Order = 0)]
    public string Result = "";

    [XmlElement(Order = 1)]
    public List<string> Messages = new List<string>();

    [XmlElement(Order = 2)]
    public List<M3Message> ResultMessage = new List<M3Message>();

    [XmlElement(Order = 3)]
    public List<CRS100MI_ListRecordResult> Record = new List<CRS100MI_ListRecordResult>();

    public CRS100MI_ListResult Parse(List<Dictionary<string, string>> list)
    {
        //parses a list of dictionaries to CRS100MI_ListRecordResult
    }
}

[Serializable()]
[XmlSerializerFormat()]
[XmlRoot(ElementName = "CRS100MI_ListRecord", DataType = "System.Xml.XmlElement", Namespace = "http://companynamespace")]
public class CRS100MI_ListRecordResult
{


    [XmlElement(Order = 0)]
    public string Result { get; set; }

    [XmlElement(Order = 1)]
    public string ErrorMessage { get; set; }

    [XmlElement(Order = 2)]
    public List<string> Messages { get; set; }

    [XmlElement(Order = 3)]
    public List<M3Message> ResultMessage { get; set; }

    [XmlElement(Order = 4)]
    public string SMCD { get; set; }

    [XmlElement(Order = 5)]
    public string TX40 { get; set; }

    [XmlElement(Order = 6)]
    public string TX15 { get; set; }

    [XmlElement(Order = 7)]
    public string SDEP { get; set; }

    [XmlElement(Order = 8)]
    public string BUAR { get; set; }

    [XmlElement(Order = 9)]
    public string PWMT { get; set; }

    [XmlElement(Order = 10)]
    public string SHOP { get; set; }

    [XmlElement(Order = 11)]
    public string LOSD { get; set; }


}

当我在微型wcf网络服务中调用此核心wcf服务并返回结果时,它将对自动生成的类进行序列化并给我

     <getCRS100StuffResult xmlns:a="http://schemas.datacontract.org/2004/07/RA_Tibco.CoreService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:PropertyChanged i:nil="true" xmlns:b="http://schemas.datacontract.org/2004/07/System.ComponentModel"/>
        <a:messagesField i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
        <a:recordField>
           <a:CRS100MI_ListRecordResult>
              <a:PropertyChanged i:nil="true" xmlns:b="http://schemas.datacontract.org/2004/07/System.ComponentModel"/>
              <a:bUARField i:nil="true"/>
              <a:errorMessageField i:nil="true"/>
              <a:lOSDField>0</a:lOSDField>
              <a:messagesField i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
              <a:pWMTField i:nil="true"/>

搜索了一下之后,我偶然发现了以下StackOverflow问题: Why does WCF sometimes add "Field" to end of generated proxy types?

因此,我确保遵循给出的说明,但是保存后,重新添加服务引用并将其添加到SOAPUI,仍然会给我带来这个问题。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

仔细查看您的CRS100MI_ListResult.cs,我发现您没有使用DataContractDataMember属性。

使用它们代替[Serializable()]属性,它应该可以正常工作。