如何在ASP.NET 4 RC WebAPI中将依赖项注入到操作过滤器中?

时间:2012-06-11 11:17:55

标签: castle-windsor asp.net-mvc-4 asp.net-web-api action-filter

我正在使用Windsor在WebAPI项目中为我的控制器管理IoC。我有一个很好地解决控制器依赖关系的DependencyResolver,但现在我想将依赖关系注入我用来管理身份验证的自定义动作过滤器。

我已经研究过使用自定义的ActionInvoker,但是从界面上看,WebAPI正在使用如何在执行之前解析自定义操作过滤器属性的属性依赖性。有人在MVC 4 RC中有一个很好的例子吗?

编辑:我知道你不能对过滤器进行构造函数注入,因为它们是属性,因此由.NET框架实例化 - 但是我希望在过滤器之后的执行生命周期中有一些点实例化,但在执行之前,我可以运行一些自定义代码来枚举过滤器的公共属性并注入必要的服务。

2 个答案:

答案 0 :(得分:10)

动作过滤器是属性。在.NET属性中,实例化过程由.NET运行时管理,您无法控制它。所以有一种可能性就是使用我个人建议你的Poor Man's Dependency Injection

另一种可能性是使用标记属性:

public class MyActionFilterAttribute : Attribute 
{ 

}

然后使用构造函数注入操作过滤器:

public class MyActionFilter : ActionFilterAttribute
{
    private readonly IFoo _foo;
    public MyActionFilter(IFoo foo)
    {
        _foo = foo;
    }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (actionContext.ActionDescriptor.GetCustomAttributes<MyActionFilterAttribute>().Any())
        {
            // The action is decorated with the marker attribute => 
            // do something with _foo
        }
    }
}

然后将其注册为Application_Start中的全局操作过滤器:

IFoo foo = ....
GlobalConfiguration.Configuration.Filters.Add(new MyActionFilter(foo));

答案 1 :(得分:4)

我遇到了同样的问题,但是为了这个而决定去ServiceLocator(DependencyResolver.GetService),因为它在框架中对我来说似乎是一种有效的方法

public class RequiresSessionAttribute :
    ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var sessionService =
            (ISessionService) actionContext
                    .ControllerContext.Configuration.DependencyResolver
                    .GetService(typeof (ISessionService));

        var sessionId = HttpUtility
            .ParseQueryString(actionContext.Request.RequestUri.Query)
            .Get("sessionId");

        if (sessionId == null
            || !sessionService.IsValid(sessionId))
            throw new SessionException();

        base.OnActionExecuting(actionContext);
    }
}

这里是对这个属性的测试,有点痛苦但可能

public class requires_sessionId
{
    [Fact]
    void can_call_action_with_session_id()
    {
        var context = GetContext("http://example.com/?sessionId=blaa");

        var sut = new RequiresSessionAttribute();

        Assert.DoesNotThrow(
            () => sut.OnActionExecuting(context));
    }

    [Fact]
    void can_not_call_action_without_session_id()
    {
        var context = GetContext("http://example.com/");

        var sut = new RequiresSessionAttribute();

        Assert.Throws<SessionException>(
            () => sut.OnActionExecuting(context));
    }

    HttpActionContext GetContext(string url)
    {
        var sessionServiceMock = new Mock<ISessionService>();
        sessionServiceMock
            .Setup(x => x.IsValid(It.IsAny<string>()))
            .Returns(true);

        var dependancyResolverMock = new Mock<IDependencyResolver>();
        dependancyResolverMock
            .Setup(x => x.GetService(It.IsAny<Type>()))
            .Returns(sessionServiceMock.Object);

        var config = new HttpConfiguration
               {
                   DependencyResolver = dependancyResolverMock.Object
               };
        var controllerContext = new HttpControllerContext
               {
                    Configuration = config,
                    Request = new HttpRequestMessage(
                               HttpMethod.Get,
                               url)
                };

        return
            new HttpActionContext
                {
                    ControllerContext = controllerContext,
                };
    }
}