卸载“eager-loaded”属性会导致返回json数据时出现问题

时间:2022-10-05 14:30:35

Hopefully the title makes sense, I'll do my best to describe my problem.

希望标题有意义,我将尽力描述我的问题。

I'm currently developing an ASP.NET Web API. In my "Company" controller I have a GetCompany() action.

我目前正在开发一个ASP。净Web API。在我的“公司”控制器中,我有一个GetCompany()动作。

    /// <summary>
    /// Gets the 'Authorization-Token' request header and finds the company with the
    /// matching decrypted token from the database, returns it; if null, returns 404
    /// </summary>
    /// <returns>Matching company for token passed in request or 404 if null</returns>
    [HttpGet][ResponseType(typeof(Company))]
    [Route("api/company/")]
    public HttpResponseMessage GetCompany() {
        string token = Request.Headers.GetValues("Authorization-Token").First();

        ICollection<Company> companies;
        Company c;
        using (BaseRepository r = new BaseRepository()) {
            companies = r.Get<Company>(null, null, null);
            c = companies.Where(cm => RSA.Decrypt(cm.Token) == token).FirstOrDefault<Company>();
        }
        if (c == null)
            return Request.CreateResponse(HttpStatusCode.NotFound, c);

        return Request.CreateResponse(HttpStatusCode.OK, c);
    }

This finds it and returns it just fine, however when I'm testing this, the response I see is

它找到它并返回它,但当我测试它时,我看到的响应是

{
    "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 'Locations' on 'System.Data.Entity.DynamicProxies.Company_40636FF93138F09772BEA689D3A3D3AB7DBB41AFF7FE3D0BEFF55FC7C1D10E0F'.",
        "ExceptionType": "Newtonsoft.Json.JsonSerializationException",
        "StackTrace": "   at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)\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.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.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)\r\n   at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext)\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__14.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()\r\n   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)\r\n   at System.Data.Entity.Core.Objects.ObjectQuery`1.Execute(MergeOption mergeOption)\r\n   at System.Data.Entity.Core.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption)\r\n   at System.Data.Entity.Core.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption)\r\n   at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.Load()\r\n   at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad()\r\n   at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject)\r\n   at System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item)\r\n   at System.Data.Entity.DynamicProxies.Company_40636FF93138F09772BEA689D3A3D3AB7DBB41AFF7FE3D0BEFF55FC7C1D10E0F.get_Locations()\r\n   at GetLocations(Object )\r\n   at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"
        }
    }
}

I understand the problem is that my properties are eager-loaded and they aren't ever loaded. I can tell it to load my navigation properties inside my Company object quite easily. The problem I'm encountering when I do that, is that it's saying the complex objects inside of those aforementioned navigation properties are null as well and I can't so easily eager load them; nor do I want to. So it's attempting to load the whole "object tree" for lack of a better way to say it (that I can think of).

我知道问题在于我的属性是“eager-loaded”的,而且它们从来没有加载过。我可以让它很容易地在我的公司对象中加载我的导航属性。我遇到的问题是,它说这些导航属性中的复杂对象也是空的我不能很容易地加载它们;我也不想。所以它试图加载整个“对象树”,因为它没有更好的表达方式(我能想到)。

So my question is, what is the best way to get around this?

我的问题是,解决这个问题的最好方法是什么?

1 个解决方案

#1


11  

The proxy is causing the serializer to attempt to access navigational properties which are no longer available since the context has been disposed.

代理导致序列化器试图访问导航属性,由于上下文已被处理,导航属性不再可用。

Try removing the proxy and eager loading from your context when you are querying for objects that will be serialized

当您查询将被序列化的对象时,尝试从上下文删除代理和热切加载。

 context.Configuration.ProxyCreationEnabled = false;
 context.Configuration.LazyLoadingEnabled = false;

#1


11  

The proxy is causing the serializer to attempt to access navigational properties which are no longer available since the context has been disposed.

代理导致序列化器试图访问导航属性,由于上下文已被处理,导航属性不再可用。

Try removing the proxy and eager loading from your context when you are querying for objects that will be serialized

当您查询将被序列化的对象时,尝试从上下文删除代理和热切加载。

 context.Configuration.ProxyCreationEnabled = false;
 context.Configuration.LazyLoadingEnabled = false;