通过Unity自定义AuthorizeAttribute中的Web Api依赖注入不起作用

时间:2017-01-24 22:10:47

标签: asp.net-web-api dependency-injection asp.net-web-api2 unity-container authorize-attribute

我已经问过这个问题并标记为重复,但在我的设计中自定义AuthorizeAttribute中的依赖注入存在问题。我试过了 来自同一答案的几个代码。除属性外,一切都正常工作,_authenticationService始终为null。我的代码:

public class AuthenticateAttribute : AuthorizeAttribute
{
   [Dependency]
   private IAuthenticationService _authenticationService { get; set; }

   public AuthenticateAttribute()
   {
        //_authenticationService is null here, even tried this code too:
        //
   }

测试1

UnityActionFilterProvider

public class UnityActionFilterProvider : IFilterProvider
{
    private readonly IUnityContainer container;

    public UnityActionFilterProvider(IUnityContainer container)
    {
        this.container = container;
    }

    public IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration,
        HttpActionDescriptor actionDescriptor)
    {
        foreach (IActionFilter actionFilter in container.ResolveAll<IActionFilter>())
        {
            // TODO: Determine correct FilterScope
            yield return new FilterInfo(actionFilter, FilterScope.Global);
        }
    }
}

RegisterComponents

public static void RegisterComponents()
    {
        var container = new UnityContainer();
        container.RegisterType<IAuthenticationService, AuthenticationService>(new HierarchicalLifetimeManager());
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

        var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
        GlobalConfiguration.Configuration.Services.Add(typeof(IFilterProvider), new UnityActionFilterProvider(container));
        var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);
        GlobalConfiguration.Configuration.Services.Remove(typeof(IFilterProvider), defaultprovider);
}

Global.asax

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        UnityConfig.RegisterComponents();
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

测试2

* UnityFilterAttributeFilterProvider *

public class UnityFilterAttributeFilterProvider : FilterAttributeFilterProvider
{
    private IUnityContainer _container;

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

    protected override IEnumerable<FilterAttribute> GetControllerAttributes(
                ControllerContext controllerContext,
                ActionDescriptor actionDescriptor)
    {

        var attributes = base.GetControllerAttributes(controllerContext,
                                                      actionDescriptor);
        foreach (var attribute in attributes)
        {
            _container.BuildUp(attribute.GetType(), attribute);
        }

        return attributes;
    }

    protected override IEnumerable<FilterAttribute> GetActionAttributes(
                ControllerContext controllerContext,
                ActionDescriptor actionDescriptor)
    {

        var attributes = base.GetActionAttributes(controllerContext,
                                                  actionDescriptor);
        foreach (var attribute in attributes)
        {
            _container.BuildUp(attribute.GetType(), attribute);
        }

        return attributes;
    }
}

RegisterComponents

public static void RegisterComponents()
    {
        var container = new UnityContainer();
        container.RegisterType<IAuthenticationService, AuthenticationService>(new HierarchicalLifetimeManager());

        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
        var oldProvider = System.Web.Mvc.FilterProviders.Providers.Single(f => f is System.Web.Mvc.FilterAttributeFilterProvider);
        System.Web.Mvc.FilterProviders.Providers.Remove(oldProvider);
        var provider = new UnityFilterAttributeFilterProvider(container);
        container.RegisterInstance<UnityFilterAttributeFilterProvider>(provider);
        System.Web.Mvc.FilterProviders.Providers.Add(provider);
}

Global.asax

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        UnityConfig.RegisterComponents();
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

0 个答案:

没有答案