Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-03-29
3
Medium Priority
?
2,209 Views
Last Modified: 2014-04-05
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
0
Comment
Question by:RBS
  • 2
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39966652
How is Activity declared?
0
 

Accepted Solution

by:
RBS earned 0 total points
ID: 39967674
Hi:

Sorry - I just figured out what the problem was - It looks like I needed to declare a MultipleActiveResultSets = true in my web.config

RBS
0
 

Author Closing Comment

by:RBS
ID: 39979865
It was a long and complicated error message and thought trial and error was able to come up with solution
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question