3

环境:Unity 和 ASP.NET MVC WEBAPI 我按照网页上的示例http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-dependency-injection 然后做了一些修改后,我找到了一种为 Controler 注入过滤器的方法。

我在 boostrapper.cs 中使用了这段代码

var container = new UnityContainer();
container.RegisterInstance<IFilterProvider>("FilterProvider", new    FilterProvider(container));
container.RegisterInstance<IActionFilter>("LogActionFilter", new TraceActionFilter());

我添加了这个类 FilterProvider

public class FilterProvider : IFilterProvider
{
    private IUnityContainer container;

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

    public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        foreach (IActionFilter actionFilter in this.container.ResolveAll<IActionFilter>())
        {
            yield return new Filter(actionFilter, FilterScope.First, null);
        }
    }
}

所以每次通过调用实现Controller的类调用方法时,我可以看到类TraceActionFilter中调用的代码。

public class TraceActionFilter : IActionFilter
{
    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        filterContext.HttpContext.Trace.Write("OnActionExecuted");
        filterContext.HttpContext.Trace.Write("Action " + filterContext.ActionDescriptor.ActionName);
        filterContext.HttpContext.Trace.Write("Controller " + filterContext.ActionDescriptor.ControllerDescriptor.ControllerName);
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
                filterContext.HttpContext.Trace.Write("OnActionExecuting");
                filterContext.HttpContext.Trace.Write("Action " + filterContext.ActionDescriptor.ActionName);
                filterContext.HttpContext.Trace.Write("Controller " + filterContext.ActionDescriptor.ControllerDescriptor.ControllerName);
    }
} 

但我在网上找不到任何允许做同样事情的示例或正确方法:注入过滤器以添加关于 WebApi 的相同行为。find 的唯一方法是使用标签手动添加过滤器,而不是使用依赖注入。你能提供任何帮助吗?

最好的问候, 亚历山大

[第一个答案后更新] 我启动命令 Install-Package Unity.AspNet.WebApi 以允许访问 UnityConfig。我添加了 global.asax

    protected void Application_Start()
    {
        Bootstrapper.Initialise();
        UnityWebApiActivator.Start();
        ConfigureApi(GlobalConfiguration.Configuration); //line added

        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

我在方法 ConfigureApi() 中添加

    void ConfigureApi(HttpConfiguration config)
    {
        var unity = new UnityContainer();
        unity.RegisterType<ProductsApiController>();
        unity.RegisterType<CategoriesApiController>();

        unity.RegisterType<IProductService, ProductService>(
            new HierarchicalLifetimeManager());
        unity.RegisterType<IProductsDatabase, ProductsDatabase>(
             new HierarchicalLifetimeManager());

        unity.RegisterType<ICategorieService, CategorieService>(
            new HierarchicalLifetimeManager());
        unity.RegisterType<ICategoriesDatabase, CategoriesDatabase>(
             new HierarchicalLifetimeManager());

        unity.RegisterType<ILogProvider, ElmahProvider>(
             new HierarchicalLifetimeManager());

        unity.RegisterType<IRootController, RootController>(
            new HierarchicalLifetimeManager());

        config.DependencyResolver = new IoCContainer(unity);

        //ADDED PARTS
        var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
        config.Services.Add(
            typeof(System.Web.Http.Filters.IFilterProvider),
            new Filter2Provider(UnityConfig.GetConfiguredContainer()));

        var defaultprovider = providers.First(p => p is System.Web.Http.Filters.ActionDescriptorFilterProvider);
        config.Services.Remove(typeof(System.Web.Http.Filters.IFilterProvider), defaultprovider);            
    }

当我调用方法 ...api/categoriesapi/GetAll 时 TraceActionFilter 中的断点没有被调用。

任何想法?我继续搜索。

4

1 回答 1

4

您将需要创建一个自定义 System.Web.Http.Filters.FilterProvider (请注意,MVC 在不同的命名空间中具有相同名称的类):

public class UnityActionFilterProvider 
    : System.Web.Http.Filters.ActionDescriptorFilterProvider, 
      System.Web.Http.Filters.IFilterProvider
{
    private readonly IUnityContainer container;

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

    public new 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);
        }
    }
}

然后您需要在启动时设置过滤器提供程序:

var providers = 
    GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();

GlobalConfiguration.Configuration.Services.Add(
    typeof(System.Web.Http.Filters.IFilterProvider),
    new UnityActionFilterProvider(UnityConfig.GetConfiguredContainer()));

var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);

GlobalConfiguration.Configuration.Services.Remove(
    typeof(System.Web.Http.Filters.IFilterProvider), 
    defaultprovider);

您应该将上述内容与用于 ASP.NET Web API 的 Unity 引导程序一起使用

使用属性的类似问题:ASP.NET Web API - Inject Dependencies Into ActionFilterAttribute

由于您使用容器来维护过滤器列表,因此您可以直接注册 FilterInfo 而不是 IActionFilter。

于 2013-08-20T01:05:27.003 回答