asp.net-mvc – 在Global.asax中注入依赖项
发布时间:2020-05-25 19:16:48 所属栏目:asp.Net 来源:互联网
导读:我正在使用MVC3和Ninject启动Web应用程序.在Global.asax文件中我还需要一个依赖项,它需要是一个单例. 我认为应该是这样的: public class MvcApplication : NinjectHttpApplication{ IUserAuthentication _auth; public MvcApplication() {
|
我正在使用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为null. 然后我尝试这样: 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,我想单例实例在我的应用程序范围内不会是单例. 我该怎么办?使用静态变量? 解决方法我们就是这样做的,我做了一些测试,我的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;
}
}
}
}
}
}
希望能帮助到你! (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.Net MVC 4窗体与2提交按钮/操作
- asp.net-mvc – 如何在MVC视图中使用Bootstrap按钮插件
- asp.net-mvc – 在asp.net MVC中使用的TempData集合是什么?
- asp.net-mvc – ASP.Net MVC – 处理不好的URL参数
- asp.net-mvc – 高级ASP路由教程和示例
- 从asp.net代码后面读表单认证cookie
- asp.net – Telerik RadAjaxManager仍然回发
- asp.net – Internet Explorer 9中的URL查询字符串值的UTF-
- asp.net – 在另一个控件之前插入控件
- 什么是最好的ASP.NET性能计数器来监视?
推荐文章
站长推荐
- 记一道毫无思路的算法题
- ASP.NET Membership API强制更改密码
- ASP.NET MVC 2中的asp.net-mvc-2 – LazyList vs
- asp.net-mvc-routing – 在.NET MVC 4.0 URL结构
- asp.net-mvc – 如何使用activedirectorymembers
- ASP.NET中的%%(嵌入式代码块)
- asp.net-mvc – 使用表单身份验证在ASP.NET MVC上
- asp.net-mvc – ASP.NET MVC验证消息未被本地化
- asp.net-mvc – 可以在mvc JsonResult控制器方法
- 为什么每个人都在ASP.NET Webforms中依赖注入是困
热点阅读
