使用ASP.New MVC 4 Web Api进行授权过滤依赖注入

时间:2014-03-25 22:56:58

标签: asp.net-mvc asp.net-mvc-4 dependency-injection unity-container asp.net-web-api

我正在尝试在MVC 4 Web Api授权过滤器上实现依赖注入。我创建了一个继承自ActionDescriptorFilterProvider的FilterProvider:

public class UnityWebApiFilterAttributeFilterProvider : ActionDescriptorFilterProvider, System.Web.Http.Filters.IFilterProvider
{
    private readonly IUnityContainer _container;

    public UnityWebApiFilterAttributeFilterProvider(IUnityContainer container)
    {
        _container = container;
    }

    public IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
    {
        var filters = base.GetFilters(configuration, actionDescriptor);

        this.BuildUpAttributes(filters);

        return filters;
    }

    private void BuildUpAttributes(IEnumerable filterInfo)
    {
        foreach (FilterInfo filter in filterInfo)
        {
            object o = _container.BuildUp(filter.GetType(), filter);
        }
    }
}

我将此提供程序设置为Global.asax文件中的默认提供程序:

var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
        //Remove the default provider
        var defaultprovider = providers.Single(i => i is ActionDescriptorFilterProvider);
        GlobalConfiguration.Configuration.Services.Remove(typeof(System.Web.Http.Filters.IFilterProvider), defaultprovider);
        //Add our custom provider
        GlobalConfiguration.Configuration.Services.Add(typeof(System.Web.Http.Filters.IFilterProvider), new UnityWebApiFilterAttributeFilterProvider(new UnityContainer()));

我还在AuthorizationFilter中添加了一个依赖项:

public class WebApplicationApiAuthorizeAttribute : System.Web.Http.AuthorizeAttribute
{

    [Dependency]
    public IAuthorizationHelper AuthorizationHelper { get; set; }

    #region OnAuthorization
    public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
    //Authorization code, which uses the AuthorizationHelper lives here
}   

}

我在Unity配置文件中设置了一个映射:

container.RegisterType<WebApplicationApiAuthorizeAttribute>(new InjectionMember[]
            {
                new InjectionProperty("AuthorizationHelper", new ResolvedParameter<IAuthorizationHelper>())
            });

        container.RegisterType<IAuthorizationHelper, WebAuthorizationHelper>(new InjectionMember[]
            {
                new InjectionProperty("Cache", new ResolvedParameter<ICache>(cacheType)),
                new InjectionProperty("UserSessionManager", new ResolvedParameter<IUserSessionManager>()),
                new InjectionProperty("AuthorizationPermissionManager", new ResolvedParameter<IAuthorizationPermissionManager>()),
                new InjectionProperty("ExceptionLogger", new ResolvedParameter<IExceptionLogger>()),
            });

我可以看到UnityWebApiFilterAttributeFilterProvider类中的“BuildUpAttributes”方法已执行,并且过滤器已传递到_container.BuildUp(...)方法,但是,WebApplicationApiAuthorizeAttribute中的“IAuthorizationHelper”依赖关系无法解析。当我调试WebApplicationApiAuthorizeAttribute时,它为null。

那么为什么这种依赖性没有被解决?

1 个答案:

答案 0 :(得分:3)

您的代码中有两个错误。

  1. 您正在构建FilterInfo对象而不是Instance

    foreach (FilterInfo filter in filterInfo)
    {
        object o = _container.BuildUp(filter.GetType(), filter);
    }
    

    但这是正确的代码:

    foreach (FilterInfo filter in filterInfo)
    {
        _container.BuildUp(filter.Instance.GetType(), filter.Instance);
    }
    
  2. 您的自定义UnityWebApiFilterAttributeFilterProvider会获得一个新的空容器:

    GlobalConfiguration.Configuration.Services.Add(
        typeof(System.Web.Http.Filters.IFilterProvider), 
        new UnityWebApiFilterAttributeFilterProvider(new UnityContainer()));
    

    但是你应该传递应用程序的配置容器:

    GlobalConfiguration.Configuration.Services.Add(
        typeof(System.Web.Http.Filters.IFilterProvider), 
        new UnityWebApiFilterAttributeFilterProvider(container));