通过Ninject的HttpContext给了我一个奇怪的生产错误c#

时间:2016-01-05 09:03:32

标签: c# asp.net .net ninject

我已经通过Ninject注入了HttpContextBase:

Kernel.Bind<HttpContextBase>()
      .ToMethod(ctx => HttpContext.Current == null ? (HttpContextBase)new NullHttpContext() : new HttpContextWrapper(HttpContext.Current))
      .InScope(HybridScope);

我的HybridScope是这样的:

private static object HybridScope(IContext ctx)
{
  return (object)HttpContext.Current ?? ctx;
}

空的NullHttpContext类是这样的:

public class NullHttpContext : HttpContextBase
{

}

我有一行CDN服务:

if (_httpContext.GetType().Name == "NullHttpContext" || (_httpContext.GetType().Name != "NullHttpContext" && _httpContext.Request.IsSecureConnection) || !Enabled) return url;

这会在_httpContext.Request.IsSecureConnection上将此错误抛给我:

  

异常:System.ArgumentException消息:值不会下降   在预期范围内。来源:mscorlib at   System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(的Int32   errorCode,IntPtr errorInfo)at   System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(字符串   姓名)在System.Web.Hosting.IIS7WorkerRequest.IsSecure()at    - \ CDNService.GetUrl(String url,   布尔缓存) - \ ICDNService.cs:第59行

我一直试图搞清楚这一点,即使在Google上也无法找到任何解决方案。

那里有没有人,谁能解决这个问题,或者可以解释为什么会这样?

如果我直接使用HttpContext.Current,它没有给我任何错误。 但是,如果我这样做,它会在Ninject无法释放实例期间导致巨大的内存泄漏。

提前致谢。

0 个答案:

没有答案
相关问题