Link to home
Start Free TrialLog in
Avatar of RBS
RBS

asked on

WebAPI 2 - Adding related object generates JSON error when retrieving values

Hi:

I am using WebAPI 2.1.  I have a simple class defined as follows:

 public class Statement
    {
        public Statement(int id, string name)
        {
            Id = id;
            Name= name;
        }

        //For EF
        public Statement()
        {

        }
        public int Id { get; set; }
        public string Name { get; set; }
    }
      

Open in new window


When I make an http request, such as mysite.com/api/statements, it works fine and retrieves the data.  When I add the the following property to my class -
        public virtual Activity Activity { get; set; } - and make a get request, I get the following error message

Message: "An error has occurred.",
ExceptionMessage: "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.",
ExceptionType: "System.InvalidOperationException",
StackTrace: null,
InnerException: {
Message: "An error has occurred.",
ExceptionMessage: "Error getting value from 'Activity' on 'System.Data.Entity.DynamicProxies.Statement_6244758049322F2013F45504488584D62A3E3D45B5CDB4D16F0EA721E9E737B1'.",
ExceptionType: "Newtonsoft.Json.JsonSerializationException",.........:

Any suggestions on what I am doing wrong greatly appreciated.

RBS
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

How is Activity declared?
ASKER CERTIFIED SOLUTION
Avatar of RBS
RBS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RBS
RBS

ASKER

It was a long and complicated error message and thought trial and error was able to come up with solution