在 Global.asax 中进行 Ninject 依赖项注入

11

我正在使用MVC3和Ninject开始一个Web应用程序。有一个依赖项,我还需要在Global.asax文件中使用,它需要成为单例。

我认为应该像这样:

public class MvcApplication : NinjectHttpApplication
{
    IUserAuthentication _auth;

    public MvcApplication()
    {
        base.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest);
    }

    protected override IKernel CreateKernel()
    {
        var _kernel = new StandardKernel(new SecurityModule());
        _auth = _kernel.Get<IUserAuthentication>();

        return _kernel;
    }

    void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
    {
        _auth.ToString();
    }

但后来我发现在调用MvcApplication_AuthenticateRequest_auth为空。

然后我尝试了这样的代码:

public class MvcApplication : NinjectHttpApplication
{
    ItUserAuthentication _auth;
    IKernel _kernel;

    public MvcApplication()
    {
        _kernel = new StandardKernel(new SecurityModule());
        _auth = _kernel.Get<IUserAuthentication>();
        base.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest);
    }

    protected override IKernel CreateKernel()
    {
        return _kernel;
    }

    void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
    {
        _auth.ToString();
    }

但现在我可以看到构造函数被调用了多次,因此我将有几个IKernel实例,我想单例实例在我的应用程序范围内不会是真正的单例。

我该怎么做?使用静态变量吗?

5个回答

10

这就是我们的做法,我进行了一些测试,我的AuthService似乎只在他的控制器中执行一次:

public class MvcApplication : NinjectHttpApplication
    {

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

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

        }

        protected override IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecutingAssembly());

            kernel.Bind<ISession>().To<MongoSession>().InRequestScope();
            kernel.Bind<IAuthenticationService>().To<AuthenticationService>().InSingletonScope();
            kernel.Bind<IMailer>().To<Mailer>().InRequestScope();
            kernel.Bind<IFileProvider>().To<MongoFileProvider>().InRequestScope();

            return kernel;
        }

        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            if (HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        var id = (FormsIdentity) HttpContext.Current.User.Identity;
                        var ticket = id.Ticket;
                        var authToken = ticket.UserData;
                        var authService = (IAuthenticationService)DependencyResolver.Current.GetService(typeof(IAuthenticationService));
                        var user = authService.GetUserForAuthToken(authToken);
                        if (user != null)
                        {
                            user.SetIdentity(HttpContext.Current.User.Identity);
                            HttpContext.Current.User = (IPrincipal) user;
                        }
                    }
                }
            }
        }
}

希望能对你有所帮助!


每个请求中调用DependencyResolver,这不是一个杀手吗? - vtortola
我不这么认为,@Remo应该比我更清楚,但由于我在大多数控制器的构造函数中注入了我的_authService,所以它可能会做同样的事情,并且不会“花费”太多... - VinnyG
太好了。我认为在@Remo修复问题之前,这样做就可以了。非常感谢。 - vtortola

5

MVC扩展默认注入HttpApplication。但是只能使用属性注入!因此,只需添加一个带有Inject属性修饰的属性即可。


2
@vtortola,你是否正在使用最新版本的Ninject Mvc3?属性应该像这样: [Inject] public ItUserAuthentication Auth { get; set; } - Talljoe
1
@vtortola 我看了一下代码。由于引入了NuGet包,这个功能已经被破坏了。由于我正在将NuGet包更新到NuGet 1.2,所以我会在接下来的几天内修复它。 - Remo Gloor
顺便说一句,当你修好它的时候,在这里给我打个招呼:D。非常感谢:) - vtortola
@vtortola 已在 Ninject.Extensions.MVC3 V2.2.2.0 中修复。 - Remo Gloor
1
将一个具有请求生命周期的对象注入到具有更长生命周期的对象中是没有意义的。一个对象应该只依赖于具有相同或更长生命周期的对象。 - Remo Gloor
显示剩余7条评论

0

在初始化IOC时,在ASP.NET MVC中如果你设置了依赖项解析器,例如使用Simple Injector:

DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

然后在 global.asax 中,您可以使用 DependencyResolver 来获取服务,例如:

var penTestService = DependencyResolver.Current.GetService<IPenTestService>();

-1
将代码从构造函数移动到Application_Start方法中。我相信即使创建了多个HttpApplication实例,也只会调用一次Application_Start,而且仅在第一个实例上调用。如果已解决您的问题,请告诉我。
这些是您在Global.asax.cs中可能拥有的各种事件处理程序:
public class Global : System.Web.HttpApplication
{
    public Global()
    {
        InitializeComponent();
    }   

    protected void Application_Start(Object sender, EventArgs e)
    {

    }

    protected void Session_Start(Object sender, EventArgs e)
    {

    }

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {

    }

    protected void Application_EndRequest(Object sender, EventArgs e)
    {

    }

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {

    }

    protected void Application_Error(Object sender, EventArgs e)
    {

    }

    protected void Session_End(Object sender, EventArgs e)
    {

    }

    protected void Application_End(Object sender, EventArgs e)
    {

    }

    #region Web Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    }
    #endregion
}

不是这样的。我已经做过了,当调用MvcApplication_AuthenticateRequest时,_auth仍然为空:( - vtortola

-1
你能使用 HttpApplication.Appliction 属性吗?
public class MyHttpApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        this.Application["auth"] = GetAuthFromContainer();
    }

    protected void Application_AuthenticateRequest()
    {
        IUserAuthentication auth = (IUserAuthentication)this.Application["auth"]; 
        // auth != null
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接