Always have error "The ObjectContent 1 type failed to serialize the response body..."
Solution 1
Change IEnumerable<Message
> to List<Message>
public IEnumerable<Message> GetAllMsg()
{
return repo.GetAll();
}
to
public List<Message> GetAllMsg()
{
return repo.GetAll();
}
UPDATE:
But beware of getting OutOfMemoryException
because this method will store all Message
objects in local memory so you have to implement some kind of paging.
Solution 2
I had the same problem and this is the solution I found
After updating the Entity Data Model you have to set ProxyCreationEnabled
to false
in your Model
Configuration.ProxyCreationEnabled = false;
My example:
public partial class EventsEntities : DbContext
{
public EventsEntities()
: base("name=EventsEntities")
{
Configuration.ProxyCreationEnabled = false;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
}
Solution 3
For these kinds of data querying you should definitely create paging for results. You have 2 options for paging in Web API.
The first option you could use OData to return IQueryable object from your action method. So that, your action supports paging.
The second option is to create a controller which supports paging. I put one example below.
[HttpGet]
public List<Book> Books(int page = 0 , int size = 100){
using(var context = new BooksDataContext()){
List<Book> books = context.Books.OrderBy(t=> t.CreateDate).Skip(page * size).Take(size).ToList();
return books;
}
}
The code above supports paging and you are able to set collection count that is going to return from the client side.
Solution 4
I had the same issue with Chrome, not so much with IE. In order to fix it I used the following lines at the Global.asax.cs, Application_Start() method:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
Related videos on Youtube
kaboom
Updated on June 04, 2022Comments
-
kaboom almost 2 years
I use Web api to retrieve data from the database. I only have 1 table "tblMessage" and want to get data from that table.
I set everything up but then when I run the website. the error always say
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml
I read some posts on stackoverflow that sayid the error could be fixed by telling the browser to output data in json format. After that, the error becomes
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json
I have tried all solutions from the following posts, but they dont fix the problem ( browser reports the same error)
Web API Error: The 'ObjectContent`1' type failed to serialize the response body for content type
Failed to serialize the response body for content type
Web API Error: The 'ObjectContent`1' type failed to serialize the response body for content type
What exactly this error is?
public interface IMessage { IQueryable<Message> GetAll(); } public class Message { [Key] public int i_StmID { get; set; } public string vch_MsgString { get; set; } } public class EFDBContext : DbContext { public DbSet<Message> Message { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Message>().ToTable("tblMessage"); } } public class MessageRepository : IMessage { private EFDBContext context = new EFDBContext(); public IQueryable<Message> GetAll() { return context.tblMessage; } } public class MessageController : ApiController { public IMessage repo = new MessageRepository(); public IEnumerable<Message> GetAllMsg() { return repo.GetAll(); } }
-
kaboom over 10 yearsThanks. I read a post about this problem and added the same thing but the browser reports the same error. I think the error generates from the code
-
kaboom over 10 yearsThanks again. Unfortunately, my code doesn't compile b/c context.tblMessage return a DbSet, not List. I tried "return context.tblMessage.ToList()" but it failed.
-
kaboom over 10 yearsactually it compiled now. but it throws OutOfMemoryException. how can I fix this?
-
Nick over 10 years@kaboom actually yes it will throw OutOfMemExcep because you are trying to store all messages in the memory, you should implement some kind of paging example use GetAllMsg().Skip(page-1*pageSize).Take(pageSize)
-
ta.speot.is over 10 yearsIs it bad to use
IQueryable<T>
instead?