It could be likely that your db.Customer is actually pointing to a dataset as opposed to a Queryable collection and because of this a Queryable collection may not be being returned (and thus might be be getting serialized properly. Adding the AsQueryable()
call after the collection could help with this :
public IQueryable<Customer> GetCustomer()
{
return db.Customer.AsQueryable();
}
Likewise if you were returning an IEnumerable collection, you could use AsEnumerable() or AsList() respectively :
public IEnumerable<Customer> GetCustomer()
{
return db.Customer.AsEnumerable();
}
How are you currently outputting the data? Could you post the code related to that? (As it looks like the data is actually all there in the response, so it may just be an issue of how you are outputting it).
How are you currently outputting the data? Could you post the code related to that? (As it looks like the data is actually all there in the response, so it may just be an issue of how you are outputting it).
after many test i found what is making the problem
i try to view data from customer table which is has relationship with 3 tables (City, Government, Type) - i try to remove the relation in the class code as:
public partial class Customer
{
public int CustomerID { get; set; }
[StringLength(100)]
public string CustomerName { get; set; }
public int TypeID { get; set; }
public int GovernmentID { get; set; }
public int CityID { get; set; }
[Column(TypeName = "date")]
public DateTime? MirageDate { get; set; }
//public virtual City City { get; set; }
//public virtual Government Government { get; set; }
//public virtual Type Type { get; set; }
}
and run with code:
// GET: api/ManageCustomersInfoAPI
public IQueryable<Customer> GetCustomers()
{
return db.Customers;
}
i got the data and no error message,
but when i uncomment the relation code and run i got the message again ?
full 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":"Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Customer_672C4B56C7A9E354053F1DC583D5B6D29714E7E5687DEB6679EEACE1339A58CD'. Path '[0].city.customers'.", "exceptionType":"Newtonsoft.Json.JsonSerializationException","stackTrace":" at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"}}
so please what is the problem here and how can i make it works ?
__________________
Best Regards
Eng.Ahmed Amin
ahmed.mo.amin@gmail.com
public partial class Customer
{
public int CustomerID { get; set; }
[StringLength(100)]
public string CustomerName { get; set; }
public int TypeID { get; set; }
public int GovernmentID { get; set; }
public int CityID { get; set; }
[Column(TypeName = "date")]
public DateTime? MirageDate { get; set; }
[JsonIgnore]
public virtual City City { get; set; }
[JsonIgnore]
public virtual Government Government { get; set; }
[JsonIgnore]
public virtual Type Type { get; set; }
}
so please what is the problem here and how can i make it works ?
"":"Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Customer_672C4B56C7A9E354053F1DC583D5B6D29714E7E5687DEB6679EEACE1339A58CD'. Path '[0].city.customers'.","
Looks like City also has a Customers List which in turn each customer will have city again thus creating self referencing loops. You should create a Customer View model without virtual navigation properties. so Json can serialize them without looping.
Or alternatively use just [JsonIgnore] as suggested.
I would suggests that you never return directly dbmodels to view like this and rely on viewmodels. dbmodels are for processing data and view models are for showing data on view. better to not send db models to the views.
With Regards,
Krunal Parekh
Krunal
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue.
so please what is the problem here and how can i make it works ?
"":"Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Customer_672C4B56C7A9E354053F1DC583D5B6D29714E7E5687DEB6679EEACE1339A58CD'. Path '[0].city.customers'.","
Looks like City also has a Customers List which in turn each customer will have city again thus creating self referencing loops. You should create a Customer View model without virtual navigation properties. so Json can serialize them without looping.
Or alternatively use just [JsonIgnore] as suggested.
I would suggests that you never return directly dbmodels to view like this and rely on viewmodels. dbmodels are for processing data and view models are for showing data on view. better to not send db models to the views.
With Regards,
Krunal Parekh
Hi do you mean create view model as:
public partial class Customer_VM
{
public int CustomerID { get; set; }
[StringLength(100)]
public string CustomerName { get; set; }
public int TypeID { get; set; }
public int GovernmentID { get; set; }
public int CityID { get; set; }
[Column(TypeName = "date")]
public DateTime? MirageDate { get; set; }
}
i try it and it works but the problem is i need to get city name (as example ?!) so how here i can get city name without virtual navigation properties
or create other properties in viewmodel like "CityName" and get "CityName" by CityID ?
__________________
Best Regards
Eng.Ahmed Amin
ahmed.mo.amin@gmail.com
Only included the properties you need. for example you need cityname from city correct than do it like this.
public partial class Customer_VM
{
public int CustomerID { get; set; }
[StringLength(100)]
public string CustomerName { get; set; }
public int TypeID { get; set; }
public int GovernmentID { get; set; }
public int CityID { get; set; }
[Column(TypeName = "date")]
public DateTime? MirageDate { get; set; }
public string CityName {get;set;}
}
Now do it like this.
public List<Customer_VM> GetCustomer()
{
var customers = db.Customers.ToList(); var response = new List<Customer_VM>();
foreach(var cust in customers) { response.Add(new Customer_VM(){ CustomerID = cust.CustomerID, // all other properties CityName = cust.City.Name }); } return response; //Here CityName is not virtual so will not cause exception. }
With Regards,
Krunal Parekh
Krunal
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue.
so please what is the problem here and how can i make it works ?
"":"Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Customer_672C4B56C7A9E354053F1DC583D5B6D29714E7E5687DEB6679EEACE1339A58CD'. Path '[0].city.customers'.","
Looks like City also has a Customers List which in turn each customer will have city again thus creating self referencing loops. You should create a Customer View model without virtual navigation properties. so Json can serialize them without looping.
Or alternatively use just [JsonIgnore] as suggested.
I would suggests that you never return directly dbmodels to view like this and rely on viewmodels. dbmodels are for processing data and view models are for showing data on view. better to not send db models to the views.
With Regards,
Krunal Parekh
Thanks Krunal
will i have create 2 viewmodel one for city and other for customer and all have virtual navigation properties and type code as:
public IQueryable<Customers_VM> GetCustomer()
{
return db.Customers.Select(i => new Customers_VM()
{
CustomerID = i.CustomerID,
GroupID = i.GroupID,
CustomerName = i.CustomerName,
City = new City_VM() { CityName = i.City.CityName },
Type = new Type_VM() { TypeName = i.Type.TypeName },
MirageDate = i.MirageDate
});
}
and run and i got all data i need - so why viewmodel work and don't give any error but dbmodels give the error befor ?
and last question please: i try to use viewmodel in edit but i found that i must set value of all properties manual in my code as:
Member
51 Points
449 Posts
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/js...
Jul 02, 2015 08:58 PM|a.amin|LINK
Hi
i have add new web api controller and work with EF v6.1.3 Code First from Database, and when i try to get all customers with:
but i didn't get any data and i got this err on console :
i try:
but nothing - so please how can i make it works without any err ?
api iqueryable get Json serialize
Best Regards
Eng.Ahmed Amin
ahmed.mo.amin@gmail.com
Contributor
4407 Points
1264 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 02, 2015 09:07 PM|mostafasydney|LINK
Try using
Hope this will help.
Mostafa
If this post helps you to resolve your problem, don't forget to "Mark as Answer"
All-Star
114593 Points
18503 Posts
MVP
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 02, 2015 09:13 PM|Rion Williams|LINK
It could be likely that your db.Customer is actually pointing to a dataset as opposed to a Queryable collection and because of this a Queryable collection may not be being returned (and thus might be be getting serialized properly. Adding the AsQueryable() call after the collection could help with this :
Likewise if you were returning an IEnumerable collection, you could use AsEnumerable() or AsList() respectively :
Member
51 Points
449 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 02, 2015 09:49 PM|a.amin|LINK
i try both and same result and same message ?
Best Regards
Eng.Ahmed Amin
ahmed.mo.amin@gmail.com
Contributor
4407 Points
1264 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 02, 2015 10:08 PM|mostafasydney|LINK
Could you please try adding these lines in WebApiConfig class under the Register method.
Thanks,
Mostafa
If this post helps you to resolve your problem, don't forget to "Mark as Answer"
Member
50 Points
25 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 03, 2015 04:04 AM|Rocky Web API|LINK
Seems we have two way to resolve this issue.
1. change the method return value to Queryable<Customer> or List<customer>, dont use IQueryable.
2. Set the Json handler to support that. Check this link: http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_PreserveReferencesHandling.htm
Send me mails: rocky_msn@hotmail.com
Member
51 Points
449 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 03, 2015 07:41 AM|a.amin|LINK
when i try both code and run - i got result as:
as you see it displayed first row only and when i do breakpoint in c# i see 44 rows (that is true 44 rows) ??!!
Best Regards
Eng.Ahmed Amin
ahmed.mo.amin@gmail.com
All-Star
114593 Points
18503 Posts
MVP
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 03, 2015 08:01 AM|Rion Williams|LINK
How are you currently outputting the data? Could you post the code related to that? (As it looks like the data is actually all there in the response, so it may just be an issue of how you are outputting it).
Member
51 Points
449 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 03, 2015 08:13 AM|a.amin|LINK
i use angularjs for that:
Best Regards
Eng.Ahmed Amin
ahmed.mo.amin@gmail.com
Contributor
2290 Points
493 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 03, 2015 01:59 PM|santhoshje|LINK
Try without filter text. Most probably filter text is restricting data. What is the value of searchText?
Member
51 Points
449 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 03, 2015 07:22 PM|a.amin|LINK
i try that but it's not related the problem still has same message ?
Best Regards
Eng.Ahmed Amin
ahmed.mo.amin@gmail.com
Contributor
2290 Points
493 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 03, 2015 11:35 PM|santhoshje|LINK
Try returning custom response object like this
CustomerResponse
Member
51 Points
449 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 06, 2015 04:39 PM|a.amin|LINK
after many test i found what is making the problem
i try to view data from customer table which is has relationship with 3 tables (City, Government, Type) - i try to remove the relation in the class code as:
and run with code:
i got the data and no error message,
but when i uncomment the relation code and run i got the message again ?
full error message:
so please what is the problem here and how can i make it works ?
Best Regards
Eng.Ahmed Amin
ahmed.mo.amin@gmail.com
Contributor
2290 Points
493 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 06, 2015 07:26 PM|santhoshje|LINK
Please use [JsonIgnore] for virtual properties.
All-Star
15252 Points
2074 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 06, 2015 11:58 PM|Krunal Parekh|LINK
Hello a.amin,
"":"Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Customer_672C4B56C7A9E354053F1DC583D5B6D29714E7E5687DEB6679EEACE1339A58CD'. Path '[0].city.customers'.","
Looks like City also has a Customers List which in turn each customer will have city again thus creating self referencing loops. You should create a Customer View model without virtual navigation properties. so Json can serialize them without looping.
Or alternatively use just [JsonIgnore] as suggested.
Please see: http://stackoverflow.com/questions/19467673/entity-framework-self-referencing-loop-detected
I would suggests that you never return directly dbmodels to view like this and rely on viewmodels. dbmodels are for processing data and view models are for showing data on view. better to not send db models to the views.
With Regards,
Krunal Parekh
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue.
Member
51 Points
449 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 07, 2015 03:38 AM|a.amin|LINK
Hi do you mean create view model as:
i try it and it works but the problem is i need to get city name (as example ?!) so how here i can get city name without virtual navigation properties
or create other properties in viewmodel like "CityName" and get "CityName" by CityID ?
Best Regards
Eng.Ahmed Amin
ahmed.mo.amin@gmail.com
All-Star
15252 Points
2074 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 07, 2015 04:25 AM|Krunal Parekh|LINK
Hello a.amin,
Only included the properties you need. for example you need cityname from city correct than do it like this.
Now do it like this.
With Regards,
Krunal Parekh
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue.
Member
51 Points
449 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 08, 2015 07:30 PM|a.amin|LINK
Thanks Krunal
will i have create 2 viewmodel one for city and other for customer and all have virtual navigation properties and type code as:
and run and i got all data i need - so why viewmodel work and don't give any error but dbmodels give the error befor ?
and last question please: i try to use viewmodel in edit but i found that i must set value of all properties manual in my code as:
but i have more that 30 column and that mean i must set them alll and to get values her i must use:
so are there is any way to make it work as this code:
that give all values automaticly and don't need to use [0] in angularjs code
Best Regards
Eng.Ahmed Amin
ahmed.mo.amin@gmail.com
All-Star
15252 Points
2074 Posts
Re: The 'ObjectContent`1' type failed to serialize the response body for content type 'applicatio...
Jul 09, 2015 09:32 PM|Krunal Parekh|LINK
Hello a.amin,
Wouldn't this code return just one customer since filter by id?
just change the return type to Customers_VM and Do FirstOrDefault() instead of tolist.
I have already explained this before because of circular references in navigation properties in your case.
Hope this helps.
With Regards,
Krunal
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue.