Include Unity in Web API Filter Attributes

I have Unity working perfectly for all the controllers in my ASP.NET Web API project - just using the default setting that exits the NuGet field. I also managed to connect it to the MVC Filter Attributes - but it doesn't seem to be able to do the same for the ASP.NET Web API filter attributes.

How to extend this default implementation to insert a dependency in an ActionFilterAttribute, for example ...

public class BasicAuthenticationAttribute : ActionFilterAttribute { [Dependency] public IMyService myService { get; set; } public BasicAuthenticationAttribute() { } } 

This filter is applied to controllers using attributes:

 [BasicAuthentication] 

I'm sure I need to connect the Unity container so that it handles the creation of the attribute class, but it needs to know where to start, since it does not use the same extensibility points as MVC filters.

I just wanted to add, other things I tried include the location of the service, not the dependency injection, but the DependencyResolver you return is not the one you are setting.

 // null var service = actionContext.Request.GetDependencyScope().GetService(typeof(IMyService)); 

or

 // null var service = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IApiUserService)); 
+4
source share
1 answer

The problem is that the Attribute class is created by .NET, and not using the WebAPI environment.

Before reading further, have you forgot to configure your DependencyResolver using your IApiUserService?

 (IUnityContainer)container; container.RegisterType<IApiUserService, MyApiUserServiceImpl>(); ... var service = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IApiUserService)); 

I created the class App_Start \ UnityConfig, which contains my UnityContainer:

 public class UnityConfig { #region Unity Container private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() => { var container = new UnityContainer(); RegisterTypes(container); return container; }); /// <summary> /// Gets the configured Unity container. /// </summary> public static IUnityContainer GetConfiguredContainer() { return container.Value; } #endregion public static void Configure(HttpConfiguration config) { config.DependencyResolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer()); } /// <summary>Registers the type mappings with the Unity container.</summary> /// <param name="container">The unity container to configure.</param> /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks> private static void RegisterTypes(IUnityContainer container) { // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. // container.LoadConfiguration(); // TODO: Register your types here // container.RegisterType<IProductRepository, ProductRepository>(); container.RegisterType<MyClass>(new PerRequestLifetimeManager(), new InjectionConstructor("connectionStringName")); } } 

UnityDependencyResolver and PerRequestLifetimeManager came from this blog post and Unity.WebApi ( Project / Nuget Package ), which I learned. (since this is bootstrap)

When I need to use UnityContainer in my other code, I passed it to the constructor:

 config.Filters.Add(new MyFilterAttribute(UnityConfig.GetConfiguredContainer())); 
+7
source

Source: https://habr.com/ru/post/1485951/


All Articles