1

您好,当我运行以下代码时出现错误 500(内部服务器错误)。我的问题是我根本没有任何错误的痕迹。视觉工作室似乎无法捕捉到它。

如果我尝试将人员添加到候选人代码失败并且我收到错误 500,则以下代码将返回候选人。问题是PersonAddressDescription实现 AddressDescription 是继承问题吗?

public class CheckController : ApiController
{
    public Candidate Get()
    {
        PersonAddressDescription pers = new PersonAddressDescription();

        Candidate candidate = new Candidate();

        //IF I REMOVE THIS NO PROBLEM
        candidate.address = pers;

        return candidate;
    }
}

地址描述类

/// <remarks/>
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(CompanyAddressDescription))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(PersonAddressDescription))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.17626")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.crif-online.ch/webservices/crifsoapservice/v1.00")]
    public abstract partial class AddressDescription : object, System.ComponentModel.INotifyPropertyChanged {

        private Location locationField;

        private ContactItem[] contactItemsField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=0)]
        public Location location {
            get {
                return this.locationField;
            }
            set {
                this.locationField = value;
                this.RaisePropertyChanged("location");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("contactItems", Order=1)]
        public ContactItem[] contactItems {
            get {
                return this.contactItemsField;
            }
            set {
                this.contactItemsField = value;
                this.RaisePropertyChanged("contactItems");
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

实现 AddressDescription 的 PersonAddressDescription 类

/// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.17626")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.crif-online.ch/webservices/crifsoapservice/v1.00")]
    public partial class PersonAddressDescription : AddressDescription {

        private string firstNameField;

        private string lastNameField;

        private string maidenNameField;

        private Sex sexField;

        private bool sexFieldSpecified;

        private string birthDateField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=0)]
        public string firstName {
            get {
                return this.firstNameField;
            }
            set {
                this.firstNameField = value;
                this.RaisePropertyChanged("firstName");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=1)]
        public string lastName {
            get {
                return this.lastNameField;
            }
            set {
                this.lastNameField = value;
                this.RaisePropertyChanged("lastName");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=2)]
        public string maidenName {
            get {
                return this.maidenNameField;
            }
            set {
                this.maidenNameField = value;
                this.RaisePropertyChanged("maidenName");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=3)]
        public Sex sex {
            get {
                return this.sexField;
            }
            set {
                this.sexField = value;
                this.RaisePropertyChanged("sex");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool sexSpecified {
            get {
                return this.sexFieldSpecified;
            }
            set {
                this.sexFieldSpecified = value;
                this.RaisePropertyChanged("sexSpecified");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=4)]
        public string birthDate {
            get {
                return this.birthDateField;
            }
            set {
                this.birthDateField = value;
                this.RaisePropertyChanged("birthDate");
            }
        }
    }
4

1 回答 1

3

我怀疑您检索到的对象 ( addResp) 在其对象图中的某处包含循环引用。循环引用不能被 JSON 序列化。

例如,尝试将以下代码放入您的控制器中,以测试当您尝试 JSON 序列化此实例时会发生什么:

TypeIdentifyAddressResponse addResp = ws.identifyAddress("test");
string json = JsonConvert.SerializeObject(addResp);


更新:

这似乎AddressDescription是一个抽象类,而您的实际实例是PersonAddressDescription. [KnownType]您需要使用属性向序列化程序指示:

[KnownType(typeof(PersonAddressDescription))]
[KnownType(typeof(CompanyAddressDescription))]
...
public abstract partial class AddressDescription : object, System.ComponentModel.INotifyPropertyChanged {
{
    ...
}

作为替代方案,如果您不想用其他属性进一步污染(已经污染的)域模型,您也可以在您的内部定义已知类型WebApiConfig.cs

config.Formatters.XmlFormatter.SetSerializer<Candidate>(
    new DataContractSerializer(typeof(Candidate),
    new Type[] { typeof(PersonAddressDescription) }));
于 2012-10-19T07:45:08.870 回答