繁体   English   中英

ASP.NET MVC-从Session_Start访问IoC容器

[英]Asp.Net mvc - accessing IoC container from Session_Start

我想管理与http://fishbowl.pastiche.org/2004/01/19/persistent_login_cookie_best_practice/

我想在会话开始时检查cookie,验证用户是否存在cookie,并在每次新会话开始时将其交换为新cookie。 如果不存在,我也想创建一个。

这是为了照顾“记住我”类型的功能-类似于SO的工作方式。

为此,我需要能够从global.asax的Session_Start方法中从容器中提取服务。 在调试应用程序时,我逐步完成了构建容器的Application_Start方法。 一切顺利,并创建了global.asax的Container属性。 但是当我进入Session_Start时-容器为空。

是否发生了我不知道的事情? 有没有更好的方法可以做到这一点?

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

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            Container = new WindsorContainer().AddFacility<WcfFacility>()
                .Install(Configuration.FromXmlFile("Configuration\\Windsor.config"))
                .Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory, "SonaTribe*.dll")));


        }

        /// <summary>
        /// See http://fishbowl.pastiche.org/2004/01/19/persistent_login_cookie_best_practice/
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Session_Start(object sender, EventArgs e)
        {
            if (Container != null)
            {
                var accountService = Container.Resolve<IAccountService>();
                var logger = Container.Resolve<ILogger>();
                var forms = Container.Resolve<IFormsAuthentication>();

                // if there is a cookie
                if (Context.Request.Cookies["user-id"] != null)
                {
                    try
                    {
                        //get the new cookie key from the server
                        var newUserSessionResponse = accountService.RegisterNewUserSession(new RegisterNewUserSessionRequest
                        {
                            SessionId = Context.Request.Cookies["user-id"].Value
                        });

                        if (newUserSessionResponse.Success)
                        {
                            //do something
                        }
                        else
                        {
                            logger.Info("Failed attaching the user to the session", newUserSessionResponse.Message);
                        }
                    }
                    catch (Exception exc)
                    {
                        logger.Error("Failed attaching the user to the session", exc);
                    }
                }
                else
                {
                    //new user:
                   //do things
                } 
            }
        }

谢谢

w://

将容器的声明(移到一个静态类中)并确保它(容器)是静态的。

实施IContainerAccessor时,标准做法是将容器存储为全局HttpApplication的静态变量。 请参阅http://hammett.castleproject.org/?p=233以获取参考。

如果不使用静态变量,则当ASP.NET运行时处理HttpApplication(运行时内部维护HttpApplication实例池)时,容器将丢失。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM