jjc99
jjc99

Reputation: 3579

Web API Error: The 'ObjectContent`1' type failed to serialize the response body for content type

I am getting this error when attempting to use a Web API controller.

Web API Error: The 'ObjectContent`1' type failed to serialize the response body for content type

the code in my controller is as follows

public IEnumerable<Student> GetAllStudents()
    {
        var allstudents = unitOfWork.StudentRepository.Get(includeProperties: "Groups");


        return allstudents;
    }

    public Student GetStudentByID(Guid id)
    {
        return unitOfWork.StudentRepository.GetByID(id);
    }

and my 'Student' class is as follows

public partial class Student
{
    public Student()
    {
        this.Groups = new HashSet<Group>();
    }

    public System.Guid StudentID { get; set; }
    public string Surname { get; set; }
    public string FirstName { get; set; }
    public byte[] Timestamp { get; set; }

    public virtual Course Course { get; set; }
    public virtual ICollection<Group> Groups { get; set; }
}

Both methods result in the same error.

My inner exception is as follows

Type 'System.Data.Entity.DynamicProxies.Student_4C97D068E1AD0BA62C3C6E441601FFB7418AD2D635F7F1C14B64F4B2BE32DF9A' with data contract name 'Student_4C97D068E1AD0BA62C3C6E441601FFB7418AD2D635F7F1C14B64F4B2BE32DF9A:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

I have a feeling I need to use the KnownType attribute but I'm not exactly sure how to implement it.

Any help would be appreciated

Upvotes: 4

Views: 10795

Answers (2)

Edward Brey
Edward Brey

Reputation: 41718

If you don't need the lazy-loaded navigation properties provided by the proxy class (System.Data.Entity.DynamicProxies.Student_4C97D068E1A...), you can disable their generation by setting:

unitOfWork.Configuration.ProxyCreationEnabled = false;

What to do if you need the proxy class is another question.


Follow these links for a good overview of lazy loading and proxies:

I usually disable lazy loading and proxies by default, and enable one or both in specific code blocks that need them.

Upvotes: 7

Maggie Ying
Maggie Ying

Reputation: 10185

What is the inner exception message? The inner exception message will be the actual exception that is thrown by the serializer and it should tell us which type is causing the exception.

Let me guess -- Is it any the type Course and the type Group? If so, try putting KnownType attribute on the actual implementation type of your class Student

[KnownType(typeof(GroupA))]
[KnownType(typeof(CourseA))]
public partial class Student
{...}

public class GroupA : Group {...}
public class CourseA : Course {...}

public interface Group {...}
public interface Course {...}

Upvotes: 1

Related Questions