Unity DI in AuthorisationFilter WebApi

本文关键字:WebApi AuthorisationFilter in DI Unity | 更新日期: 2023-09-27 18:15:06

我在这里使用这个链接作为参考;

https://unity.codeplex.com/discussions/446780

所以根据链接,我添加了一个UnityActionFilterProvider类;

public class UnityActionFilterProvider : ActionDescriptorFilterProvider, IFilterProvider
{
    private readonly IUnityContainer container;
    public UnityActionFilterProvider(IUnityContainer container)
    {
        this.container = container;
    }
    public new IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
    {
        var filters = base.GetFilters(configuration, actionDescriptor);
        foreach (var filter in filters)
        {
            container.BuildUp(filter.Instance.GetType(), filter.Instance);
        }
        return filters;
    }
}

,然后我添加到我的UnityConfig.cs

            public static void RegisterFilterProviders(IUnityContainer UnityDependencyResolver)
    {
        var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
        GlobalConfiguration.Configuration.Services.Add(
            typeof(IFilterProvider),
            new UnityActionFilterProvider(UnityDependencyResolver));
        var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);
        GlobalConfiguration.Configuration.Services.Remove(typeof(IFilterProvider), defaultprovider);
    }

然后在Startup.cs

中调用
   public void Configuration(IAppBuilder app)
    {
        HttpConfiguration = new HttpConfiguration();
        LoggingConfig.RegisterLogger();
        ConfigureOAuth(app);
        var unityContainer = UnityConfig.GetConfiguredContainer();
        HttpConfiguration.DependencyResolver = new UnityDependencyResolver(unityContainer);
        //for DI in the filters
        UnityConfig.RegisterFilterProviders(unityContainer);
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(HttpConfiguration);
        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(HttpConfiguration);
    }

最后,我有以下过滤器(注意:我也尝试过这个作为ActionFilter,看看这是否有区别)

public class HasPermissionAttribute : AuthorizationFilterAttribute
{
    [Dependency]
    public UserPermissionService UserService { get; set; }

    public override void OnAuthorization(HttpActionContext actionContext)
    {
    }
}

然而,UserService是空的,当这个被击中?关于我在这里做错了什么,有什么想法吗?

Unity DI in AuthorisationFilter WebApi

希望这能在某一天帮助到别人。

基本上都是因为我在API中使用了OAuth。

因此,我只需要通过编辑startup.cs来确保配置通过过滤器,如下所示;

        HttpConfiguration = new HttpConfiguration();
        LoggingConfig.RegisterLogger();
        ConfigureOAuth(app);
        var unityContainer = UnityConfig.GetConfiguredContainer();
        HttpConfiguration.DependencyResolver = new UnityDependencyResolver(unityContainer);
        //for DI in the filters
        UnityConfig.RegisterFilterProviders(unityContainer, HttpConfiguration);

,然后简单地用;

修改注册
    public static void RegisterFilterProviders(IUnityContainer UnityDependencyResolver, HttpConfiguration configuration)
    {
        var providers = configuration.Services.GetFilterProviders().ToList();
        configuration.Services.Add(
            typeof(IFilterProvider),
            new UnityActionFilterProvider(UnityDependencyResolver));
        var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);
        configuration.Services.Remove(typeof(IFilterProvider), defaultprovider);
    }