asp.net-mvc – 在Global.asax中注入依赖项

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 在Global.asax中注入依赖项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用MVC3和Ninject启动Web应用程序.在Global.asax文件中我还需要一个依赖项,它需要是一个单例.

我认为应该是这样的:

  1. public class MvcApplication : NinjectHttpApplication
  2. {
  3. IUserAuthentication _auth;
  4.  
  5. public MvcApplication()
  6. {
  7. base.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest);
  8. }
  9.  
  10. protected override IKernel CreateKernel()
  11. {
  12. var _kernel = new StandardKernel(new SecurityModule());
  13. _auth = _kernel.Get<IUserAuthentication>();
  14.  
  15. return _kernel;
  16. }
  17.  
  18. void MvcApplication_AuthenticateRequest(object sender,EventArgs e)
  19. {
  20. _auth.ToString();
  21. }

但是当我调用MvcApplication_AuthenticateRequest时,我看到_auth为null.

然后我尝试这样:

  1. public class MvcApplication : NinjectHttpApplication
  2. {
  3. ItUserAuthentication _auth;
  4. IKernel _kernel;
  5.  
  6. public MvcApplication()
  7. {
  8. _kernel = new StandardKernel(new SecurityModule());
  9. _auth = _kernel.Get<IUserAuthentication>();
  10. base.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest);
  11. }
  12.  
  13. protected override IKernel CreateKernel()
  14. {
  15. return _kernel;
  16. }
  17.  
  18. void MvcApplication_AuthenticateRequest(object sender,EventArgs e)
  19. {
  20. _auth.ToString();
  21. }

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

我该怎么办?使用静态变量?

解决方法

我们就是这样做的,我做了一些测试,我的AuthService似乎只进入他的控制器一次:
  1. public class MvcApplication : NinjectHttpApplication
  2. {
  3.  
  4. public static void RegisterRoutes(RouteCollection routes)
  5. {
  6. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  7.  
  8. routes.MapRoute(
  9. "Default",// Route name
  10. "{controller}/{action}/{id}",// URL with parameters
  11. new { controller = "Home",action = "Index",id = UrlParameter.Optional } // Parameter defaults
  12. );
  13.  
  14. }
  15.  
  16. protected override IKernel CreateKernel()
  17. {
  18. var kernel = new StandardKernel();
  19. kernel.Load(Assembly.GetExecutingAssembly());
  20.  
  21. kernel.Bind<ISession>().To<MongoSession>().InRequestScope();
  22. kernel.Bind<IAuthenticationService>().To<AuthenticationService>().InSingletonScope();
  23. kernel.Bind<IMailer>().To<Mailer>().InRequestScope();
  24. kernel.Bind<IFileProvider>().To<MongoFileProvider>().InRequestScope();
  25.  
  26. return kernel;
  27. }
  28.  
  29. protected override void OnApplicationStarted()
  30. {
  31. base.OnApplicationStarted();
  32.  
  33. AreaRegistration.RegisterAllAreas();
  34. RegisterRoutes(RouteTable.Routes);
  35. }
  36.  
  37. protected void Application_AuthenticateRequest(Object sender,EventArgs e)
  38. {
  39. if (HttpContext.Current.User != null)
  40. {
  41. if (HttpContext.Current.User.Identity.IsAuthenticated)
  42. {
  43. if (HttpContext.Current.User.Identity is FormsIdentity)
  44. {
  45. var id = (FormsIdentity) HttpContext.Current.User.Identity;
  46. var ticket = id.Ticket;
  47. var authToken = ticket.UserData;
  48. var authService = (IAuthenticationService)DependencyResolver.Current.GetService(typeof(IAuthenticationService));
  49. var user = authService.GetUserForAuthToken(authToken);
  50. if (user != null)
  51. {
  52. user.SetIdentity(HttpContext.Current.User.Identity);
  53. HttpContext.Current.User = (IPrincipal) user;
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }

希望能帮助到你!

猜你在找的asp.Net相关文章