zcz527 / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

Property injection broken for global ActionFilters. #357

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Create and register a global ActionFilter.  Give it a property to which you 
want a dependency injected.  Configure AutoFac correctly.  Run.  No injection.

Original issue reported on code.google.com by abe.gill...@gmail.com on 4 Apr 2012 at 9:21

GoogleCodeExporter commented 8 years ago
Hi Abe - by register, do you mean with MVC or with Autofac? Autofac won't get 
involved at all in the management of components you register directly with MVC 
this way - try registering the filter with the ContainerBuilder only 
(configuring property injection) and let me know if this helps. Cheers! Nick

Original comment by nicholas...@gmail.com on 5 Apr 2012 at 2:02

GoogleCodeExporter commented 8 years ago
Registration with AutoFac.  Go through standard registration a la 
http://alexmg.com/post/2011/03/24/FilterAttribute-Property-Injection-in-Autofac-
MVC-3-Integration.aspx

But instead of applying CustomActionFilter via an attribute decoration, 
register it with MVC as a global ActionFilter:

Application_Start()
{
  GlobalFilters.Filters.Add(new CustomActionFilter());
  ...
}

Property injection will not work (it does work for non-global ActionFilters 
just fine).

Original comment by abe.gill...@gmail.com on 5 Apr 2012 at 10:20

GoogleCodeExporter commented 8 years ago
MVC will use the single instane added to the global filters collection for all 
invocations. You could use constructor injection for your global filter and 
resolve it from the container before adding it to the collection. Just keep in 
mind that the instance you add will be the only one used so no per-request 
lifetime scoping will be applied.

Original comment by alex.meyergleaves on 11 Apr 2012 at 3:26