I have a custom HTTP Module. I would like to inject the logger using my IoC framework, so I can log errors in the module. However, of course I don't get a constructor, so can't inject it into that. What's the best way to go about this?
If you need the specific IoC container - I'm currently using Windsor, but may soon move to AutoFac.
Thanks
You could pass in the required dependencies via the HttpApplication context passed to you by the Init method...
I just answered this question on my blog.
See also http://lozanotek.com/blog/archive/2009/08/19/Autowire_IHttpModules_with_IoC.aspx
I am curious about andrey-tsykunov's answer, but don't have the rep to comment on it.
I am trying to get comfortable with IoC and DI, so I may be missing something, but wouldn't it be simpler to use IContainerProviderAccessor from within MyModule, rather than create another module?
For example:
First time I saw dependency injection to HttpModules in Spring.NET (not advertising this framework though). The idea is that you have special HttpModule which injects dependencies to other application-level HttpModule-s.
Unfortunatelly current version of Autofac.Integration.Web does not support this, but you can easily do that yourself:
HttpModuleInjectionModule should be registered before other HttpModule-s in web.config:
I'm sure you can do similar things in Windsor. The difference would be in how you access your root container from HttpModuleInjectionModule.