aspnet / AspNetWebFormsDependencyInjection

Dependency injection support for ASP.NET Web Forms 4.x

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Constructor Injection for HttpModules

arrayofletters opened this issue · comments

Constructor injection doesn't seem to work for HttpModules, even if a constructor is marked with the [InjectionConstructor] attribute. Property injection does work.

commented

How are you adding your HttpModule classes to the pipeline?

If I remember correctly, constructor injection for HttpModules has never been supported by Unity or ASP.NET because HttpModules are managed differently, for example, in IIS' Managed Pipeline mode ASP.NET doesn't control the HttpModules, instead IIS does.

Also, in the Unity.WebForms examples (including my own, at https://github.com/Jehoel/Unity.WebForms ), it isn't possible at all (by default) because the configuration and registration of the Unity container happens in the PostApplicationStart event whereas WebActivatorEx adds HttpModules inside the PreApplicationStart event (though I think it might be possible if the Unity container was configured inside the PreApplicationStart event - but you'd have to be careful to ensure the container set-up code runs before any HttpModule registrations - and you'd need to reimplement DynamicModuleUtility so it uses the Unity container (instead of the default Activator) to construct the HttpModules.

Good to know. I wasn't sure if I was simply doing something wrong (e.g. incorrect configuration somewhere) or if it was something not currently supported. Thanks for your insight.

commented

@arrayofletters I'm wrong - you can get HttpModule constructor DI injection using WebObjectActivator - provided that...:

  1. WebObjectActivator (and your DI system) is configured inside your project's WebActivatorEx.PreApplicationStartMethod
  2. WebObjectActivator (and your DI system) is configured before calling Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule( typeof( YourHttpModule) )

I don't know if this works with HttpModules registered in your web.config file's <system.web> or <system.webServer> elements - all of my projects are using DynamicModuleUtility.RegisterModule.