我正在尝试在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。
那么为什么这种依赖性没有被解决?
答案 0 :(得分:3)
您的代码中有两个错误。
您正在构建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);
}
您的自定义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));