Arno 2501
Arno 2501

Reputation: 9397

Web api HTTP 500 (Internal Server Error)

Hello I have an error 500 (internal server error) when I run the code below. My issue is that I have no trace at all of the error. It seems that visual studio is unable to catch it.

The following code returns a Candidate if I try to add pers to candidate the code fail and i get error 500. The thing is PersonAddressDescription implement AddressDescription is inheritance the problem ?

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;
    }
}

AddressDescription class

/// <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));
            }
        }
    }

PersonAddressDescription class that implement AddressDescription

/// <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");
            }
        }
    }

Upvotes: 1

Views: 2056

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

I suspect that the object you retrieved (addResp) contains circular references somewhere in its object graph. Circular references cannot be JSON serialized.

For example try putting the following code inside your controller to test what happens when you attempt to JSON serialize this instance:

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


UPDATE:

It seems that AddressDescription is an abstract class and your actual instance is PersonAddressDescription. You need to indicate that to the serializer by using the [KnownType] attribute:

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

As an alternative if you don't want to further pollute your (already polluted) domain models with other attributes you could also define the known type inside your WebApiConfig.cs:

config.Formatters.XmlFormatter.SetSerializer<Candidate>(
    new DataContractSerializer(typeof(Candidate),
    new Type[] { typeof(PersonAddressDescription) }));

Upvotes: 3

Related Questions