问题描述
我想实现一个MVC 4网页API授权过滤器依赖注入。我创建了一个从ActionDescriptorFilterProvider继承的FilterProvider:
I'm trying to implement dependency injection on a MVC 4 Web Api Authorization Filter. I created a FilterProvider that inherits from ActionDescriptorFilterProvider:
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文件的默认提供:
and I set this provider to be the default provider in my Global.asax file:
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的依赖关系:
I also added a dependency in the 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
}
}
和我成立了一个映射在我的统一配置文件:
and I set up a mapping in my Unity configuration file:
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。
I can see that the "BuildUpAttributes" method in the UnityWebApiFilterAttributeFilterProvider class is executed and that the filter is passed into the _container.BuildUp(...) method, however, the "IAuthorizationHelper" dependency in the WebApplicationApiAuthorizeAttribute does not get resolved. It is null, when I debug the WebApplicationApiAuthorizeAttribute.
那么,为什么这种依赖性不会得到解决?
So why is this dependency not being resolved?
推荐答案
有在code两个错误。
There are two errors in your code.
-
您正在构建了
FilterInfo
对象,而不是它的实例的:
foreach (FilterInfo filter in filterInfo)
{
object o = _container.BuildUp(filter.GetType(), filter);
}
但是,这是正确的code:
But this is the correct code:
foreach (FilterInfo filter in filterInfo)
{
_container.BuildUp(filter.Instance.GetType(), filter.Instance);
}
您自定义的 UnityWebApiFilterAttributeFilterProvider
得到一个新的空容器:
Your custom UnityWebApiFilterAttributeFilterProvider
gets a new and empty container:
GlobalConfiguration.Configuration.Services.Add(
typeof(System.Web.Http.Filters.IFilterProvider),
new UnityWebApiFilterAttributeFilterProvider(new UnityContainer()));
但是,你应该通过应用程序的可配置的容器上:
But you should pass on the application's configured container:
GlobalConfiguration.Configuration.Services.Add(
typeof(System.Web.Http.Filters.IFilterProvider),
new UnityWebApiFilterAttributeFilterProvider(container));
这篇关于授权过滤器依赖注入与ASP.New MVC 4网页API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!