Asp.net mvc Web api HTTP 500(内部服务器错误)

Asp.net mvc Web api HTTP 500(内部服务器错误),asp.net-mvc,visual-studio-2010,asp.net-web-api,Asp.net Mvc,Visual Studio 2010,Asp.net Web Api,你好,当我运行下面的代码时,我有一个错误500(内部服务器错误)。我的问题是,我完全没有错误的痕迹。visual studio似乎无法捕捉到它 如果我尝试将pers添加到Candidate中,下面的代码将返回一个候选者。代码失败,得到错误500。问题是PersonalAddressDescription实现AddressDescription是继承的问题吗 public class CheckController : ApiController { public Candidate Ge

你好,当我运行下面的代码时,我有一个错误500(内部服务器错误)。我的问题是,我完全没有错误的痕迹。visual studio似乎无法捕捉到它

如果我尝试将pers添加到Candidate中,下面的代码将返回一个候选者。代码失败,得到错误500。问题是PersonalAddressDescription实现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));
            }
        }
    }
//
[System.Xml.Serialization.xmlcludeAttribute(typeof(CompanyAddressDescription))]
[System.Xml.Serialization.xmlcludeAttribute(typeof(PersonAddressDescription))]
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Xml”,“4.0.30319.17626”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(命名空间=”http://www.crif-online.ch/webservices/crifsoapservice/v1.00")]
公共抽象部分类AddressDescription:对象,System.ComponentModel.INotifyPropertyChanged{
私有位置字段;
私人联系人项目[]联系人项目字段;
/// 
[System.Xml.Serialization.XmlElementAttribute(顺序=0)]
公共场所{
得到{
返回此.locationField;
}
设置{
this.locationField=值;
本.RaiseProperty变更(“位置”);
}
}
/// 
[System.Xml.Serialization.XmlElementAttribute(“contactItems”,顺序=1)]
公共联系人项目[]联系人项目{
得到{
返回此.contactItems字段;
}
设置{
this.contactItemsField=值;
此项。RaisePropertyChanged(“contactItems”);
}
}
公共事件System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
受保护的void RaisePropertyChanged(字符串propertyName){
System.ComponentModel.PropertyChangedEventHandler propertyChanged=this.propertyChanged;
如果((propertyChanged!=null)){
propertyChanged(这是新的System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
实现AddressDescription的PersonalAddressDescription类

/// <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");
            }
        }
    }
//
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Xml”,“4.0.30319.17626”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(命名空间=”http://www.crif-online.ch/webservices/crifsoapservice/v1.00")]
公共部分类PersonalAddressDescription:AddressDescription{
私有字符串firstNameField;
私有字符串lastNameField;
私有字符串maidenNameField;
私人性领域;
指定的专用布尔值;
私有字符串birthDateField;
/// 
[System.Xml.Serialization.XmlElementAttribute(顺序=0)]
公共字符串名{
得到{
返回此.firstNameField;
}
设置{
this.firstNameField=值;
本.RaiseProperty变更(“名字”);
}
}
/// 
[System.Xml.Serialization.XmlElementAttribute(顺序=1)]
公共字符串姓氏{
得到{
返回此.lastname字段;
}
设置{
this.lastNameField=值;
此.RaisePropertyChanged(“姓氏”);
}
}
/// 
[System.Xml.Serialization.XmlElementAttribute(顺序=2)]
公共字符串少女名{
得到{
返回此.maidenNameField;
}
设置{
this.maidenNameField=值;
此。RaisePropertyChanged(“MaideName”);
}
}
/// 
[System.Xml.Serialization.XmlElementAttribute(顺序=3)]
公共性{
得到{
返回此.sexField;
}
设置{
this.sexField=值;
本.提高财产改变(“性”);
}
}
/// 
[System.Xml.Serialization.XmlIgnoreAttribute()]
公共图书馆{
得到{
返回指定的此字段;
}
设置{
this.sexFieldSpecified=值;
本.RaisePropertyChanged(“sexSpecified”);
}
}
/// 
[System.Xml.Serialization.XmlElementAttribute(顺序=4)]
公共字符串生日{
得到{
返回此.birthDateField;
}
设置{
this.birthDateField=值;
本.RaiseProperty变更(“出生日期”);
}
}
}
我怀疑您检索到的对象(
addResp
)在其对象图的某处包含循环引用。循环引用不能被JSON序列化

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

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


更新:

似乎
AddressDescription
是一个抽象类,而您的实际实例是
PersonalAddressDescription
。您需要使用以下属性向序列化程序指示:

[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) }));
config.Formatters.XmlFormatter.SetSerializer(
新的DataContractSerializer(typeof(候选者),
新类型[]{ty