Returning AspNetUser table data from get request


Returning AspNetUser table data from get request



I am using Asp.Net Web Api Identity - Individual User Account. I am returning AspNetUser Table Data from Web API.
Note : This table is made my asp.net itself, not me. But, I changed its connection string to store it in a pre-exiting database in Sql, and now, returning table data causes Internal Server Error.


Internal Server Error


public AspNetUser Get(string id)
{
using (UserDBEntities entities = new UserDBEntities())
{
return entities.AspNetUsers.FirstOrDefault(u => u.UserName == id);
}
}



And class for AspNetUser table is default, as individual user account proposes.



If I do it like below, error still persist on normal run, but not while debugging, that is when I debug the code step-by-step, it works file, and returns correct data, but when I remove my breakpoints and execute OR press continue while debugging instead of going step wise to return statement, it runs into error.


AspNetUser data = entities.AspNetUsers.FirstOrDefault(u => u.UserName == id);
return data;



Unlike this scenario, Returning without storing data, runs into error in all cases (debugging or not).



But, I don't understand, why??
how come this can affect anyhow?



Network Error Response Body:=>


{"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 'AspNetUserClaims' on 'System.Data.Entity.DynamicProxies.AspNetUser_4C1FDBB9E07016D6C527FBA4F5785F2C7FE37C77A86A5D1946B99CFFDE56DB7F'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":" at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)rn at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)rn at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)rn at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)rn at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)rn at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)rn at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)rn at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)rn at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)rn at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__22.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.","ExceptionType":"System.ObjectDisposedException","StackTrace":" at System.Data.Entity.Core.Objects.ObjectContext.get_Connection()rn at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)rn at System.Data.Entity.Core.Objects.ObjectQuery`1.Execute(MergeOption mergeOption)rn at System.Data.Entity.Core.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption)rn at System.Data.Entity.Core.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption)rn at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad()rn at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject)rn at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item)rn at System.Data.Entity.DynamicProxies.AspNetUser_4C1FDBB9E07016D6C527FBA4F5785F2C7FE37C77A86A5D1946B99CFFDE56DB7F.get_AspNetUserClaims()rn at GetAspNetUserClaims(Object )rn at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"}}}



I want to know the reason of this anomaly, not any solution, because it can be solved by returning a defined class, Like this =>


User user = new User();
AspNetUser data = entities.AspNetUsers.FirstOrDefault(u => u.UserName == id);
user.reg_username = data.UserName;
user.reg_email = data.Email;
return user;





You are missing all the other tables Identity creates
– Stilgar
Jun 30 at 20:10





@Stilgar Yes, I understand that, but I don't need those tables, and if that is the case, why I don't get any error while debugging step-wise. And, how to correct it?
– Abhishek Jaiswal
Jun 30 at 20:13






Seems like an issue with lazy loading. When Web API tries to serialize the object it hits the properties which do not exist. Doesn't really explain why it works with a debugger though. Maybe the debugger hits the properties and silently swallows the exception and then EF treats them as loaded...
– Stilgar
Jun 30 at 23:05









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API