asp.net-mvc 带有 Ninject 和 Filter 属性的依赖注入,用于 asp.net mvc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6193414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dependency Injection with Ninject and Filter attribute for asp.net mvc
提问by Shawn Mclean
I'm writing a custom Authorization Filter for asp.net mvc 3. I need to inject a userservice into the class but I have no idea how to do this.
我正在为 asp.net mvc 3 编写自定义授权过滤器。我需要将用户服务注入到类中,但我不知道如何做到这一点。
public class AuthorizeAttribute : FilterAttribute, IAuthorizationFilter
{
private IUserService userService;
private string[] roles;
public AuthorizeAttribute(params string[] roles)
{
this.roles = roles;
}
public void OnAuthorization(AuthorizationContext filterContext)
{
throw new NotImplementedException();
}
}
I'm using ninject for dependency injection. I do not want to use a Factory or service locator pattern.
我正在使用 ninject 进行依赖注入。我不想使用工厂或服务定位器模式。
My bindings look like this in the global.acsx:
我的绑定在 global.acsx 中如下所示:
internal class SiteModule : NinjectModule
{
public override void Load()
{
Bind<IUserService>().To<UserService>();
}
}
回答by B Z
See this answer:
看到这个答案:
Custom Authorization MVC 3 and Ninject IoC
If you want to use constructor injection then you need to create an attribute and a filter.
如果你想使用构造函数注入,那么你需要创建一个属性和一个过滤器。
///marker attribute
public class MyAuthorizeAttribute : FilterAttribute { }
//filter
public class MyAuthorizeFilter : IAuthorizationFilter
{
private readonly IUserService _userService;
public MyAuthorizeFilter(IUserService userService)
{
_userService = userService;
}
public void OnAuthorization(AuthorizationContext filterContext)
{
var validUser = _userService.CheckIsValid();
if (!validUser)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "action", "AccessDenied" }, { "controller", "Error" } });
}
}
}
Binding:
捆绑:
this.BindFilter<MyAuthorizeFilter>(System.Web.Mvc.FilterScope.Controller, 0).WhenControllerHas<MyAuthorizeAttribute>();
Controller:
控制器:
[MyAuthorizeAttribute]
public class YourController : Controller
{
}
HTH...
哈...
回答by John Culviner
I would highlyrecommend B Z's answer. DO NOT use [Inject]!
我强烈推荐 BZ 的回答。不要使用[注射]!
I used an [Inject] like Darin Dimitrov said was possible and it actually caused threading issues under high load, high contention situations in conjunction with .InRequestScope.
我使用了 [Inject],就像 Darin Dimitrov 所说的那样,它实际上在高负载、高争用情况下与 .InRequestScope 一起导致了线程问题。
B Z's way is also what is on the Wiki and I have seen many places where Remo Gloor (Ninject author) says this is the correct way to do it
BZ 的方法也是 Wiki 上的方法,我在很多地方看到 Remo Gloor(Ninject 作者)说这是正确的方法
https://github.com/ninject/ninject.web.mvc/wiki/Filter-configurations
https://github.com/ninject/ninject.web.mvc/wiki/Filter-configurations
Downvote [Inject] answers in here because seriously you will get burned (probably in production if you don't load test properly prior!)
在这里投票 [Inject] 答案,因为严重的是你会被烧毁(如果你之前没有正确加载测试,可能会在生产中!)
回答by Wolfgang
I found a simple solution for any occasion where construction is not handled by Ninject:
对于 Ninject 不处理构造的任何场合,我找到了一个简单的解决方案:
var session = (IMyUserService)DependencyResolver.Current.GetService(typeof (IMyUserService));
Actually this is exactly what I am using with my custom AuthorizeAttribute. Much easier than having to implement a separate FilterAttribute.
实际上,这正是我在自定义 AuthorizeAttribute 中使用的。比必须实现单独的 FilterAttribute 要容易得多。
回答by Darin Dimitrov
On way would be to use a property injection and decorate the property with the [Inject]
attribute:
方法是使用属性注入并使用属性装饰[Inject]
属性:
public class AuthorizeAttribute : FilterAttribute, IAuthorizationFilter
{
[Inject]
public IUserService UserService { get; set; }
private string[] roles;
...
}
Constructor injection doesn't work well with attributes as you will no longer be able to decorate controllers/actions with them. You could only use constructor injection with the filter binding syntax in NInject:
构造函数注入不适用于属性,因为您将无法再用它们装饰控制器/动作。您只能在 NInject 中使用带有过滤器绑定语法的构造函数注入:
public class AuthorizeAttribute : FilterAttribute, IAuthorizationFilter
{
private readonly IUserService userService;
private string[] roles;
public AuthorizeAttribute(IUserService userService, params string[] roles)
{
this.userService = userService;
this.roles = roles;
}
...
}
and then:
进而:
internal class SiteModule : Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<IUserService>().To<UserService>();
this.BindFilter<AuthorizeAttribute>(FilterScope.Controller, 0)
.WhenControllerType<AdminController>();
}
}
The BindFilter<>
extension method is defined in the Ninject.Web.Mvc.FilterBindingSyntax
namespace so make sure you have brought that into scope before calling it on a kernel.
该BindFilter<>
扩展方法在定义Ninject.Web.Mvc.FilterBindingSyntax
空间中,因此请确保您已经带来了进入范围调用它在内核前。