ASP.Net MVC命令的执行需要开放且可用的连接。连接的当前状态被破坏

时间:2012-08-20 19:27:08

标签: 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 connection. The connection's current state is broken.

这可能在使用该工具时随时发生。我们已经将我们的ninjects更新到最新版本并且它帮助了很多,但是当工具被大量使用时,错误更少但仍然发生。我一直只是通过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>();
    }        
}

HomeController中:

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();
    }
}

0 个答案:

没有答案