Last active
August 29, 2015 14:22
-
-
Save davidbreyer/973a5377debc079a6f15 to your computer and use it in GitHub Desktop.
The UnityActionFilterProvider class will build up custom filters in your web api project. The RegisterFilterProviders will call the buildup class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static void RegisterFilterProviders() | |
| { | |
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// Allows dependency injection to work with Web API Filters. | |
| /// </summary> | |
| public class UnityActionFilterProvider : ActionDescriptorFilterProvider, IFilterProvider | |
| { | |
| private readonly IUnityContainer container; | |
| /// <summary> | |
| /// Constructor | |
| /// </summary> | |
| /// <param name="container">Unity container from UnityConfig.</param> | |
| public UnityActionFilterProvider(IUnityContainer container) | |
| { | |
| this.container = container; | |
| } | |
| /// <summary> | |
| /// | |
| /// </summary> | |
| /// <param name="configuration"></param> | |
| /// <param name="actionDescriptor"></param> | |
| /// <returns></returns> | |
| 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment