Asp.Net API error: type "ObjectContent`1" could not serialize the response body for the content type "application / xml"; encoding = UTF-8 '

The simplest example of this, I get a collection and try to output it through the Web API:

// GET api/items public IEnumerable<Item> Get() { return MyContext.Items.ToList(); } 

And I get the error message:

Type object
'System.Data.Objects.ObjectQuery`1 [Dcip.Ams.BO.EquipmentWarranty] cannot be converted to type
'System.Data.Entity.DbSet`1 [Dcip.Ams.BO.EquipmentWarranty]

This is a fairly common error related to new proxies, and I know that I can fix it by installing:

 MyContext.Configuration.ProxyCreationEnabled = false; 

But this strikes the goal of many of my attempts. Is there a better way?

+33
asp.net-web-api entity-framework entity-framework-4
Dec 19 '12 at 18:48
source share
6 answers

I propose to disable proxy creation only in a place where you do not need, or cause you problems. You do not need to disable it all over the world, you can simply disable the current database context with the code ...

  [HttpGet] [WithDbContextApi] public HttpResponseMessage Get(int take = 10, int skip = 0) { CurrentDbContext.Configuration.ProxyCreationEnabled = false; var lista = CurrentDbContext.PaymentTypes .OrderByDescending(x => x.Id) .Skip(skip) .Take(take) .ToList(); var count = CurrentDbContext.PaymentTypes.Count(); return Request.CreateResponse(HttpStatusCode.OK, new { PaymentTypes = lista, TotalCount = count }); } 

Here I just turned off ProxyCreation in this method, because a new DBC context is created for each request, and therefore I turned off ProxyCreation for this case. Hope this helps.

+23
Apr 29 '14 at 18:33
source share

if you have navigation properties and you do not want to make them non-virtual, you should use JSON.NET and change the configuration in App_Start to use JSON not XML!
after installing JSON.NET From NuGet, paste this code in WebApiConfig.cs into the Register method

 var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; config.Formatters.Remove(config.Formatters.XmlFormatter); 
+23
Feb 15 '15 at 9:13
source share

If you have navigation properties, make them non-virtual. Mapping will still work, but this will prevent the creation of dynamic proxy objects that cannot be serialized.]

The lack of lazy loading in WebApi is great, since you don't have a permanent connection, and you ran .ToList () anyway.

+12
Aug 21 '13 at 14:28
source share

I simply disabled proxy classes as needed:

  // GET: ALL Employee public IEnumerable<DimEmployee> Get() { using (AdventureWorks_MBDEV_DW2008Entities entities = new AdventureWorks_MBDEV_DW2008Entities()) { entities.Configuration.ProxyCreationEnabled = false; return entities.DimEmployees.ToList(); } } 
+6
Oct 21 '16 at 3:32
source share

It helped me:
Add the following code to the Application_Start function Global.asax.cs

 GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; GlobalConfiguration.Configuration.Formatters .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); 
+4
Jun 05 '17 at 5:28
source share

In my case, the returned object has a property inside it with a type that did not have a constructor without arguments / by default. By adding a null argument constructor to this type, the object can be successfully serialized.

+3
Aug 12 '16 at 20:16
source share



All Articles