How to inject dependencies in an HttpModule with ninject?

I'm kind of amazed that nobody has answered this all day! Looks like I stumped you guys :) Well, I solved the issue. I wrote my own custom implementation of IHttpModule and compiled it into the Ninject.

Web assembly myself. Here is the source of the base class I added: namespace Ninject. Web { public class HttpModuleBase : IHttpModule { /// /// This method is unused by the base class.

/// public virtual void Dispose() { } /// /// Ininitialize the module and request injection. /// /// public virtual void Init(HttpApplication context) { RequestActivation(); } /// /// Asks the kernel to inject this instance. /// protected virtual void RequestActivation() { KernelContainer.

Inject(this); } } } I simply modeled it after the other base classes in the Ninject. Web assembly. It appears to be working wonderfully.

Just make your HttpModule inherit from Ninject.Web. HttpModuleBase and then you are free to use property injection within your module, like this: public class AppOfflineHttpModule : HttpModuleBase { Inject public IUtilitiesController utilitiesController { get; set; } ... } Don't feel like copying this code and re-compiling Ninject. Web yourself?

Here is a zip of the assembly and related assemblies so you have the correct versions of each. It is up to date with the latest versions as of 7/11/2011 http://www.CodeTunnel.com/Content/Downloads/Ninject.Web_withHttpModuleBase.zip.

I'm kind of amazed that nobody has answered this all day! Looks like I stumped you guys :) Well, I solved the issue. I wrote my own custom implementation of IHttpModule and compiled it into the Ninject.

Web assembly myself. Here is the source of the base class I added: namespace Ninject. Web { public class HttpModuleBase : IHttpModule { /// /// This method is unused by the base class.

/// public virtual void Dispose() { } /// /// Ininitialize the module and request injection. /// /// public virtual void Init(HttpApplication context) { RequestActivation(); } /// /// Asks the kernel to inject this instance. /// protected virtual void RequestActivation() { KernelContainer.

Inject(this); } } } I simply modeled it after the other base classes in the Ninject. Web assembly. It appears to be working wonderfully.

Just make your HttpModule inherit from Ninject.Web. HttpModuleBase and then you are free to use property injection within your module, like this: public class AppOfflineHttpModule : HttpModuleBase { Inject public IUtilitiesController utilitiesController { get; set; } ... } Don't feel like copying this code and re-compiling Ninject. Web yourself?

Here is a zip of the assembly and related assemblies so you have the correct versions of each. It is up to date with the latest versions as of 7/11/2011. http://www.CodeTunnel.com/Content/Downloads/Ninject.Web_withHttpModuleBase.zip.

Phil Haack blogged about a way to do this that makes it possible to use constructor injection and thereby avoid making your HttpModule depend directly on Ninject. In a standard NinjectHttpApplication, do the following: Step 1 Use Nuget to find and add the HttpModuleMagic package to your web project. Step 2 Write your HttpModule to use constructor injection: public class MyHttpModule : IHttpModule { public MyHttpModule(ISomeService someService) {...} } Step 3 Remove the http module from your web.

Config: Step 4 Set up bindings: Bind().To(); // Repeat the pattern above for any other modules.

Phil Haack blogged about a way to do this that makes it possible to use constructor injection and thereby avoid making your HttpModule depend directly on Ninject. In a standard NinjectHttpApplication do the following.

I'm kind of amazed that nobody has answered this all day! Looks like I stumped you guys :).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions