gpt4 book ai didi

asp.net-mvc - 使用 ASP.New MVC 4 Web Api 进行授权过滤器依赖注入(inject)

转载 作者:行者123 更新时间:2023-12-02 05:08:54 24 4
gpt4 key购买 nike

我正在尝试在 MVC 4 Web Api 授权过滤器上实现依赖项注入(inject)。我创建了一个继承自 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 时,它​​为空。

那么为什么这种依赖性没有得到解决呢?

最佳答案

您的代码中有两个错误。

  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));

关于asp.net-mvc - 使用 ASP.New MVC 4 Web Api 进行授权过滤器依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22648072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com