为什么使用System.Web.HttpContext.Current.Cache.Insert缓存对象突然导致Ninject抛出OutOfMemory异常?

时间:2016-02-22 17:03:05

标签: json ajax asp.net-mvc caching ninject

我的.NET MVC应用程序中有一些操作可以通过大量数据并将JSON对象返回给我的视图。为了加快速度,我已经开始缓存JSON对象并返回它们,而不是在短时间内通过所有数据:

System.Web.HttpContext.Current.Cache.Insert(System.Web.HttpContext.Current.User.Identity.Name + "WB", result, null, DateTime.UtcNow.AddMinutes(1), TimeSpan.Zero);

// result is the object that gets converted to JSON

result = new
{
    TopDepartures = TopDepartures.ToArray(),
    YesterdayFlights = YesterdayFlights.Count(),
    TodayFlights = TodayFlights.Count(),
    TomorrowFlights = TomorrowFlights.Count(),
    LastMonthPax = LastMonthPax.ToString("#,##0"),
    YesterdayPax = YesterdayPax.ToString("#,##0"),
    PaxMonthToDate = PaxMonthToDate.ToString("#,##0"),
    FirstCheckInLocalTime = FirstCheckIn.EstimatedCheckIn.ToString("HH:mm"),
    FirstCheckInOrigin = FirstCheckIn.OriginIATA,
    FirstCheckInDestination = FirstCheckIn.DestinationIATA,
    FirstCheckInSDT = FirstCheckIn.ScheduledDepartureTime.ToString("HH:mm"),
    FirstCheckInSAT = FirstCheckIn.ScheduledArrivalTime.ToString("HH:mm"),
    FirstCheckInFlightNo = FirstCheckIn.CarrierCode + " " + FirstCheckIn.FlightNo,
    LastCheckInLocalTime = LastCheckIn.EstimatedCheckIn.ToString("HH:mm"),
    LastCheckInOrigin = LastCheckIn.OriginIATA,
    LastCheckInDestination = LastCheckIn.DestinationIATA,
    LastCheckInSDT = LastCheckIn.ScheduledDepartureTime.ToString("HH:mm"),
    LastCheckInSAT = LastCheckIn.ScheduledArrivalTime.ToString("HH:mm"),
    LastCheckInFlightNo = LastCheckIn.CarrierCode + " " + LastCheckIn.FlightNo,
    TomorrowFirstCheckInLocalTime = TomorrowFirstCheckIn.EstimatedCheckIn.ToString("HH:mm"),
    TomorrowFirstCheckInOrigin = TomorrowFirstCheckIn.OriginIATA,
    TomorrowFirstCheckInDestination = TomorrowFirstCheckIn.DestinationIATA,
    TomorrowFirstCheckInSDT = TomorrowFirstCheckIn.ScheduledDepartureTime.ToString("HH:mm"),
    TomorrowFirstCheckInSAT = TomorrowFirstCheckIn.ScheduledArrivalTime.ToString("HH:mm"),
    TomorrowFirstCheckInFlightNo = TomorrowFirstCheckIn.CarrierCode + " " + TomorrowFirstCheckIn.FlightNo,

};

我在行动的开头做了检查:

var cachedResult = System.Web.HttpContext.Current.Cache.Get(System.Web.HttpContext.Current.User.Identity.Name + "WB");

if (cachedResult != null)
{
    return Json(cachedResult, JsonRequestBehavior.AllowGet);
}

问题是,自从我实现这个以来,Ninject一直在定期触发OutOfMemory异常。我一直在研究这个,但是我还没有找到任何明确引用缓存和Ninject OutOfMemory异常之间的链接的东西。似乎也没有导致异常被触发的特定模式。它会运行好几个小时的更改和测试,然后突然网站将停止工作,我会在日志中看到OutOfMemory异常。然后其他时间它会立即发生。

如果该信息相关,则调用此Action并通过AJAX调用检索该对象。

这是堆栈跟踪:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Collections.Generic.HashSet`1.SetCapacity(Int32 newSize, Boolean forceNewHashCodes)
   at System.Collections.Generic.HashSet`1.IncreaseCapacity()
   at System.Collections.Generic.HashSet`1.AddIfNotPresent(T value)
   at System.Collections.Generic.HashSet`1.Add(T item)
   at Ninject.Activation.Caching.ActivationCache.AddActivatedInstance(Object instance)
   at Ninject.Activation.Strategies.ActivationCacheStrategy.Activate(IContext context, InstanceReference reference)
   at Ninject.Activation.Pipeline.<>c__DisplayClass2.<Activate>b__0(IActivationStrategy s)
   at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[T](IEnumerable`1 series, Action`1 action)
   at Ninject.Activation.Pipeline.Activate(IContext context, InstanceReference reference)
   at Ninject.Activation.Context.ResolveInternal(Object scope)
   at Ninject.Activation.Context.Resolve()
   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.<CastIterator>d__94`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Ninject.Infrastructure.Language.ExtensionsForIEnumerable.ToArraySlow(IEnumerable series, Type elementType)
   at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
   at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
   at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
   at Ninject.Activation.Context.ResolveInternal(Object scope)
   at Ninject.Activation.Context.Resolve()
   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
   at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
   at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
   at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
   at Ninject.Activation.Context.ResolveInternal(Object scope)
   at Ninject.Activation.Context.Resolve()
   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at Ninject.ResolutionExtensions.Get(IResolutionRoot root, Type service, IParameter[] parameters)
   at AirlineChoiceDashboard.WebUI.Infrastructure.NinjectControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) in D:\Development\Airline Choice Dashboard\Source Code\Development\WebUI\Infrastructure\NinjectControllerFactory.cs:line 38

0 个答案:

没有答案