Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject dependencies in an HttpModule with ninject?

We are running a webforms project at my company and I have an HttpModule that I need to resolve dependencies for.

We use the Ninject.Web library to resolve dependencies for master pages, pages, user controls, web services, and HttpHandlers. All these have base classes you can inherit from in the Ninject.Web Namespace:

  • MasterPageBase
  • PageBase
  • WebServiceBase
  • HttpHandlerBase
  • And a custom one we added since for some odd reason it wasn't there: UserControlBase

However I am unable to find a HttpModuleBase. There is a NinjectHttpModule, but that is not a base class, it is a real module that tries to eliminate the need to inherit from base classes in pages and user controls, but it has some bugs and we are not using it.

What is the best way to resolve my dependencies in my HttpModule?

When I google this I come up with this question on the first page -_-

like image 548
Chev Avatar asked Jul 11 '11 16:07

Chev


2 Answers

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:

<httpModules>
    <!-- Modules will be defined via DI bindings -->
</httpModules>

Step 4

Set up bindings:

Bind<IHttpModule>().To<MyHttpModule>();
// Repeat the pattern above for any other modules.
like image 185
StriplingWarrior Avatar answered Oct 18 '22 03:10

StriplingWarrior


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
    {
        /// <summary>
        /// This method is unused by the base class.
        /// </summary>
        public virtual void Dispose()
        {

        }

        /// <summary>
        /// Ininitialize the module and request injection.
        /// </summary>
        /// <param name="context"></param>
        public virtual void Init(HttpApplication context)
        {
            RequestActivation();
        }

        /// <summary>
        /// Asks the kernel to inject this instance.
        /// </summary>
        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; }

    ...
}
like image 3
Chev Avatar answered Oct 18 '22 03:10

Chev