C# 向global.asax中注入依赖项

C# 向global.asax中注入依赖项,c#,dependency-injection,ninject,webforms,global-asax,C#,Dependency Injection,Ninject,Webforms,Global Asax,在Global.asax中注入依赖项似乎并不总是有效有时会,有时我会得到ContextDisposedException(似乎问题发生在我进行页面重定向时??)。我处于ASP.NET网络表单上下文中 这是我的密码: public class Global : HttpApplication { [Inject] public UserManager UserManager { get; set; } private void Application_PostAuthen

在Global.asax中注入依赖项似乎并不总是有效有时会,有时我会得到ContextDisposedException(似乎问题发生在我进行页面重定向时??)。我处于ASP.NET网络表单上下文中

这是我的密码:

public class Global : HttpApplication
{
    [Inject]
    public UserManager UserManager { get; set; }

    private void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated)
        {
            GlobalSecurityContext.SetPrincipal(User);

            string path = Request.AppRelativeCurrentExecutionFilePath;
            if (path.Contains(".aspx"))
            {
                // Get the current user
                var userData = UserManager.GetByIdWithLogin(User.Identity.Name);
                if (userData != null)
                {
                    LoginDataDTO data = userData.LoginData; 
                    if (data.XXX && ...)
                    {
                        Response.Redirect(...);
                    }
                }
            }
        }
    }

    protected void Session_End(Object sender, EventArgs e)
    {
        UserManager.Logout();
    }
}
在这篇文章中,MarkSeemann说不应该在global.asax中使用依赖注入,因为global.asax是合成根

既然我不想直接调用我的UserManager,因为构造函数需要一个存储库,那么解决问题的最佳方法是什么呢

public UserManager(IGenericRepository repository) : base(repository)
{
}
GenericRepository
本身有一个构造函数,它需要一个
IContext

public GenericRepository(IContext context)
{
}
我可能会做
newusermanager(newgenericrepository(newmycontext))
但是

  • 我不会对整个请求重复使用相同的上下文
  • 我需要在GUI中的AccessLayer上添加一个引用,这是我想要避免的
  • 作为信息,目前我正在注入如下上下文:

    // Dynamically load the context so that we dont have a direct reference on it!
    string contextType = // read type from web.config
    if (!String.IsNullOrEmpty(contextType))
    {
        Type context = Type.GetType(contextType);
        Bind<IContext>().To(context).InRequestScope();
    }
    
    //动态加载上下文,这样我们就不会直接引用它了!
    string contextType=//从web.config读取类型
    如果(!String.IsNullOrEmpty(contextType))
    {
    类型context=Type.GetType(contextType);
    绑定()到(上下文).InRequestScope();
    }
    
    任何帮助都将不胜感激

    [编辑]:

    按如下方式更改UserProperty属性:

    public UserManager UserManager
    {
        get { return ServiceLocator.Current.GetInstance<UserManager>(); }
    }
    
    公共用户管理器用户管理器 { get{return ServiceLocator.Current.GetInstance();} }
    在这种情况下,您可以构建您的Ninject容器,并将其用作此特定实例的服务定位器(因为您位于合成根目录中,此时无法注入任何内容)。

    我同意Global.asax是入口点-合成根目录。您在那里连接了依赖项,因此无法注入任何内容。我只需要使用IoC容器的
    Resolve
    方法来获取所需的实例。这就像在问,为什么我不能在创建DI容器之前使用它:-pst偶:DI容器是在启动时创建的,所以应该在我的PostAuthentication方法中可用,不是吗?正如我所写的,大部分时间这段代码都在工作。。。它只是有时失败。@Bidou“有时失败”不是一个很好的描述。给我们一个例外。如果你做不到,那就是你的问题了——找艾玛等等。鲁本:这就是我做的。。。“有时是这样,有时我会遇到ContextDisposedException(似乎问题发生在我执行页面时。重定向??)”