2

我们的一些内部应用程序是 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>();
    }        
}

家庭控制器:

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

0 回答 0