Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 30th, 2013  |  syntax: None  |  size: 4.59 KB  |  views: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var container = new UnityContainer();
  2. container.RegisterInstance<IFilterProvider>("FilterProvider", new    FilterProvider(container));
  3. container.RegisterInstance<IActionFilter>("LogActionFilter", new TraceActionFilter());
  4.        
  5. public class FilterProvider : IFilterProvider
  6. {
  7.     private IUnityContainer container;
  8.  
  9.     public FilterProvider(IUnityContainer container)
  10.     {
  11.         this.container = container;
  12.     }
  13.  
  14.     public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
  15.     {
  16.         foreach (IActionFilter actionFilter in this.container.ResolveAll<IActionFilter>())
  17.         {
  18.             yield return new Filter(actionFilter, FilterScope.First, null);
  19.         }
  20.     }
  21. }
  22.        
  23. public class TraceActionFilter : IActionFilter
  24. {
  25.     public void OnActionExecuted(ActionExecutedContext filterContext)
  26.     {
  27.         filterContext.HttpContext.Trace.Write("OnActionExecuted");
  28.         filterContext.HttpContext.Trace.Write("Action " + filterContext.ActionDescriptor.ActionName);
  29.         filterContext.HttpContext.Trace.Write("Controller " + filterContext.ActionDescriptor.ControllerDescriptor.ControllerName);
  30.     }
  31.  
  32.     public void OnActionExecuting(ActionExecutingContext filterContext)
  33.     {
  34.                 filterContext.HttpContext.Trace.Write("OnActionExecuting");
  35.                 filterContext.HttpContext.Trace.Write("Action " + filterContext.ActionDescriptor.ActionName);
  36.                 filterContext.HttpContext.Trace.Write("Controller " + filterContext.ActionDescriptor.ControllerDescriptor.ControllerName);
  37.     }
  38. }
  39.        
  40. protected void Application_Start()
  41.     {
  42.         Bootstrapper.Initialise();
  43.         UnityWebApiActivator.Start();
  44.         ConfigureApi(GlobalConfiguration.Configuration); //line added
  45.  
  46.         AreaRegistration.RegisterAllAreas();
  47.         WebApiConfig.Register(GlobalConfiguration.Configuration);
  48.         FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  49.         RouteConfig.RegisterRoutes(RouteTable.Routes);
  50.         BundleConfig.RegisterBundles(BundleTable.Bundles);
  51.     }
  52.        
  53. void ConfigureApi(HttpConfiguration config)
  54.     {
  55.         var unity = new UnityContainer();
  56.         unity.RegisterType<ProductsApiController>();
  57.         unity.RegisterType<CategoriesApiController>();
  58.  
  59.         unity.RegisterType<IProductService, ProductService>(
  60.             new HierarchicalLifetimeManager());
  61.         unity.RegisterType<IProductsDatabase, ProductsDatabase>(
  62.              new HierarchicalLifetimeManager());
  63.  
  64.         unity.RegisterType<ICategorieService, CategorieService>(
  65.             new HierarchicalLifetimeManager());
  66.         unity.RegisterType<ICategoriesDatabase, CategoriesDatabase>(
  67.              new HierarchicalLifetimeManager());
  68.  
  69.         unity.RegisterType<ILogProvider, ElmahProvider>(
  70.              new HierarchicalLifetimeManager());
  71.  
  72.         unity.RegisterType<IRootController, RootController>(
  73.             new HierarchicalLifetimeManager());
  74.  
  75.         config.DependencyResolver = new IoCContainer(unity);
  76.  
  77.         //ADDED PARTS
  78.         var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
  79.         config.Services.Add(
  80.             typeof(System.Web.Http.Filters.IFilterProvider),
  81.             new Filter2Provider(UnityConfig.GetConfiguredContainer()));
  82.  
  83.         var defaultprovider = providers.First(p => p is System.Web.Http.Filters.ActionDescriptorFilterProvider);
  84.         config.Services.Remove(typeof(System.Web.Http.Filters.IFilterProvider), defaultprovider);            
  85.     }
  86.        
  87. public class UnityActionFilterProvider
  88.     : System.Web.Http.Filters.ActionDescriptorFilterProvider,
  89.       System.Web.Http.Filters.IFilterProvider
  90. {
  91.     private readonly IUnityContainer container;
  92.  
  93.     public UnityActionFilterProvider(IUnityContainer container)
  94.     {
  95.         this.container = container;
  96.     }
  97.  
  98.     public new IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration,
  99.         HttpActionDescriptor actionDescriptor)
  100.     {
  101.         foreach (IActionFilter actionFilter in container.ResolveAll<IActionFilter>())
  102.         {
  103.             // TODO: Determine correct FilterScope
  104.             yield return new FilterInfo(actionFilter, FilterScope.Global);
  105.         }
  106.     }
  107. }
  108.        
  109. var providers =
  110.     GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
  111.  
  112. GlobalConfiguration.Configuration.Services.Add(
  113.     typeof(System.Web.Http.Filters.IFilterProvider),
  114.     new UnityActionFilterProvider(UnityConfig.GetConfiguredContainer()));
  115.  
  116. var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);
  117.  
  118. GlobalConfiguration.Configuration.Services.Remove(
  119.     typeof(System.Web.Http.Filters.IFilterProvider),
  120.     defaultprovider);
clone this paste RAW Paste Data