环境: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 中的断点没有被调用。
任何想法?我继续搜索。