0
kaixin created
when I Update [User] Object, and return this object. then get an 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 'CreatorUser' on 'System.Data.Entity.DynamicProxies.User_698FBAB5DA826A8924500EC1DF1F8FCC3CE3D42EFDADDFE0CFF03329EDF43ADD'.",
"exceptionType": "Newtonsoft.Json.JsonSerializationException",
"stackTrace": " 在 Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n 在 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n 在 System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)\r\n 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.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": " 在 System.Data.Entity.Core.Objects.ObjectContext.get_Connection()\r\n 在 System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)\r\n 在 System.Data.Entity.Core.Objects.ObjectQuery`1.Execute(MergeOption mergeOption)\r\n 在 System.Data.Entity.Core.Objects.DataClasses.EntityReference`1.Load(MergeOption mergeOption)\r\n 在 System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad()\r\n 在 System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject)\r\n 在 System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__2(TProxy proxy, TItem item)\r\n 在 System.Data.Entity.DynamicProxies.User_698FBAB5DA826A8924500EC1DF1F8FCC3CE3D42EFDADDFE0CFF03329EDF43ADD.get_CreatorUser()\r\n 在 GetCreatorUser(Object )\r\n 在 Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"
}
}
}
Response Code : 500
Response Headers:
{
"date": "Sun, 31 Jul 2016 18:24:19 GMT",
"server": "Microsoft-IIS/10.0",
"x-aspnet-version": "4.0.30319",
"x-powered-by": "ASP.NET",
"content-type": "application/json; charset=utf-8",
"cache-control": "private",
"x-sourcefiles": "=?UTF-8?B?RDpcUHJvamVjdFxkb25ldF9wcm9qZWN0XFZNRVMwMVxWTUVTLldlYlxhcGlcc2VydmljZXNcYXBwXHVzZXJcVXBkYXRlVXNlcg==?=",
"content-length": "4390"
}
my source code:
public async Task<User> UpdateUser(long id, CreateUserInput input)
{
var user = await _userRepository.GetAsync(id);
if (input.Password != "password")
{
user.Password = new PasswordHasher().HashPassword(input.Password);
}
user.DisplayName = input.DisplayName;
user.Mobile = input.Mobile;
user.IsActive = input.IsActive;
try
{
var result = (await _userRepository.UpdateAsync(user)).MapTo<User>();
await CurrentUnitOfWork.SaveChangesAsync();
return result;
}
catch (Exception err)
{
throw err;
}
}
1 Answer(s)
-
0
You should not return Entity. Return DTO instead: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Data-Transfer-Objects#DocDtoSerializationProblems">http://www.aspnetboilerplate.com/Pages/ ... onProblems</a>