Ninject 和 MVC3:对动作过滤器的依赖注入
我发现了大量关于如何使用 Ninject 在 ASP.NET MVC3 中的 ActionFilter 上进行属性注入的不确定文章和问题。
有人能给我一个明确的例子吗?
这是我的自定义 auth 属性。
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
[Inject]
public IService Service { get; set; }
[Inject]
public IAuthenticationHelper AuthenticationHelper { get; set; }
public override void OnAuthorization(AuthorizationContext filterContext)
{
//My custom code
}
}
我正在使用 WebActivator 来设置 Ninject
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.Web.AppStart_NinjectMvc3), "Start")]
namespace MyProject.Web {
public static class AppStart_NinjectMvc3 {
public static void RegisterServices(IKernel kernel) {
//Binding things
}
public static void Start() {
// Create Ninject DI Kernel
IKernel kernel = new StandardKernel();
// Register services with our Ninject DI Container
RegisterServices(kernel);
// Tell ASP.NET MVC 3 to use our Ninject DI Container
DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));
}
}
}
我的服务和帮助程序从未被注入。我需要改变什么?
I've found loads of inconclusive articles and questions on how to do property injection on an ActionFilter in ASP.NET MVC3 using Ninject.
Could someone give me a clear example please?
Here's my custom auth attribute.
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
[Inject]
public IService Service { get; set; }
[Inject]
public IAuthenticationHelper AuthenticationHelper { get; set; }
public override void OnAuthorization(AuthorizationContext filterContext)
{
//My custom code
}
}
I am using the WebActivator to set up Ninject
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.Web.AppStart_NinjectMvc3), "Start")]
namespace MyProject.Web {
public static class AppStart_NinjectMvc3 {
public static void RegisterServices(IKernel kernel) {
//Binding things
}
public static void Start() {
// Create Ninject DI Kernel
IKernel kernel = new StandardKernel();
// Register services with our Ninject DI Container
RegisterServices(kernel);
// Tell ASP.NET MVC 3 to use our Ninject DI Container
DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));
}
}
}
My service and helper are never injected. What do I need to change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,有比使用过滤器属性更好的解决方案。请参阅我的博文,了解使用 Ninject 声明过滤器的另一种方法。它不需要属性注入,而是使用构造函数注入:
http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/
http://www.planetgeek .ch/2011/02/22/ninject-mvc3-and-ninject-web-mvc3-merged-to-one-package/
In my opinion there is a better solution than using filter attributes. See my blogposts about an alternative way of declaring filters using Ninject. It does not require property injection and uses constructor injection instead:
http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/
http://www.planetgeek.ch/2011/02/22/ninject-mvc3-and-ninject-web-mvc3-merged-to-one-package/
以下是您可以继续的方法:
然后您可以拥有自定义授权属性:
以及用它装饰的控制器操作:
并且应该注入依赖项。
Here's how you could proceed:
and then you could have your custom authorize attribute:
and a controller action decorated with it:
and the dependencies should be injected.