C# ASP.NETMVC命令的执行需要一个开放的可用连接。连接的当前状态已断开

C# ASP.NETMVC命令的执行需要一个开放的可用连接。连接的当前状态已断开,c#,asp.net-mvc-3,ninject,C#,Asp.net Mvc 3,Ninject,我们的一些内部应用程序是MVC3,并且都使用Ninject.MVC 我们遇到了一个在某一点上影响所有工具的问题。首先我们得到一个错误: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. 其次是: Execution of the command requires an open and available connect

我们的一些内部应用程序是MVC3,并且都使用Ninject.MVC

我们遇到了一个在某一点上影响所有工具的问题。首先我们得到一个错误:

Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
其次是:

Execution of the command requires an open and available connection. The connection's current state is broken.
使用工具时,这可能随时发生。我们已经将我们的Ninject更新到最新版本,这帮了大忙,错误很少发生,但在工具大量使用时仍然会发生。我只是通过nuget软件包管理器设置了Ninject.MVC3,并将适当的服务注册到NinjectWebCommon.cs

这是我们的控制器,NinjectWebCommon和global.asax已经设置好了,我们有什么地方做错了吗

我们知道肯定会解决这个问题的一个方法是完全摆脱Ninject,并在每个控制器上实例化我们的存储库。但我猜这不是个好办法

Global.asax删除了一些路由:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Index", // Route name
            "", // URL with parameters
            new { controller = "Home", action = "Index" } // Parameter defaults
        );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }
}
NinjectWebCommon.cs:

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);
        return kernel;
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IToolRepository>().To<ToolRepository>();
    }        
}

您最好为每个控制器方法创建一个AccessManager实例,除非其他代码取决于当前方法中发生的情况。看起来最有可能的问题是全局对象在请求之间被GC化了……有些工具甚至不使用AccessManager,仍然会崩溃,但在那些使用AccessManager的工具上,这可能值得一试!如果您还没有尝试过,那么“一瞥”可能有助于确定对象何时超出范围。至少,您可能会看到异常的实际来源。谢谢,不幸的是,这个问题发生的非常随机,我们从来没有能够在我们的开发计算机上复制它。我会尝试一下,但我必须非常幸运,工具在我使用“一瞥”时崩溃了。我们还缺少一些东西。您最好为每个控制器方法创建一个AccessManager实例,除非其他代码取决于当前方法中发生的情况。看起来最有可能的问题是全局对象在请求之间被GC化了……有些工具甚至不使用AccessManager,仍然会崩溃,但在那些使用AccessManager的工具上,这可能值得一试!如果您还没有尝试过,那么“一瞥”可能有助于确定对象何时超出范围。至少,您可能会看到异常的实际来源。谢谢,不幸的是,这个问题发生的非常随机,我们从来没有能够在我们的开发计算机上复制它。我会尝试一下,但我必须非常幸运,工具在我使用“一瞥”时崩溃了。我们肯定还遗漏了什么。
public class HomeController : Controller
{
    private IBattleStationRepository _repository;
    private LoginEntities _loginEntities;
    private SpecialLib _speclib;
    private AccessManager _accessManager;


    public HomeController(IBattleStationRepository repository)
    {
        _repository = repository;
        _speclib = new SpecialLib();
        _loginEntities = new LoginEntities();
        _accessManager = new AccessManager();

    }

    public virtual ActionResult Index()
    {
        var byPassHomePage = false;
        var urlReferrer = HttpContext.Request.UrlReferrer;
        var user = _accessManager.GetUserByUserName(User.Identity.Name);
        if (urlReferrer == null)
        {
            byPassHomePage = true;
        }

        if (user.TeamId != null && byPassHomePage == true)
        {
            return RedirectToAction("Release", "Release", new { shortTeamName = user.Team.ShortName });
        }

        return View();
    }
}