9

I created a binding for HttpContextBase in my NinjectWebCommon.RegisterServices method, but when I try to reference it in my controllers or services I get an error message.

Here's the binding:

kernel.Bind<HttpContextBase>().ToMethod(context => new HttpContextWrapper(HttpContext.Current)).InRequestScope();

Here's the error message:

Error activating HttpContextBase
More than one matching bindings are available.
Activation path:
 2) Injection of dependency HttpContextBase into parameter abase of constructor of type HomeController
 1) Request for HomeController

Suggestions:
 1) Ensure that you have defined a binding for HttpContextBase only once.

If I remove the binding, then it appears to do what I wanted (resolves to HttpContextWrapper), but I'm wondering how this gets registered?

BlakeH
  • 3,354
  • 2
  • 21
  • 31
  • 1
    Why does your `HomeController` take an `HttpContextBase` constructor parameter? Why are you registering the `HttpContextBase` in your DI container? That's completely meaningless. – Darin Dimitrov Jun 06 '13 at 14:35
  • This is a bit of a contrived examplee and I understand your feelings on the matter, but I'm curious as to HOW its being wired behind the scenes. I typically control what is registered, and in this case, it appears that ninject is doing it for me. – BlakeH Jun 06 '13 at 14:41
  • There you go, you answered your question by yourself: `it appears that ninject is doing it for me`. – Darin Dimitrov Jun 06 '13 at 14:42
  • 2
    @DarinDimitrov Why is it meaningless? – Ben Aaronson Sep 24 '14 at 15:26

2 Answers2

21

but I'm wondering how this gets registered?

Look at the source code of the MvcModule.cs and your question will be immediately answered:

this.Kernel.Bind<HttpContext>().ToMethod(ctx => HttpContext.Current).InTransientScope();
this.Kernel.Bind<HttpContextBase>().ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InTransientScope();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 7
    In case anyone runs into this same problem: as of v3.2.1 the `HttpContextBase` binding [has been removed](https://github.com/ninject/ninject.web.mvc/commit/41b0136be6d265f82396d382ac87d999eb981a87) from the `MvcModule.cs` file. You must add your own binding. – Dave Oct 06 '14 at 22:58
  • 2
    You don't need your own binding, it's registered from Ninject.Web.Common here: https://github.com/ninject/Ninject.Web.Common/blob/master/src/Ninject.Web.Common/WebCommonNinjectModule.cs – Hakakou Mar 12 '16 at 14:44
  • I'm using v3.2.1 of Ninject.MVC myself, and it's still binding it's own HttpContextBase. I assume that's because even though MvcModule.cs may not be doing it anymore it's still being automatically bound in Ninject.Web.Common which Ninject.MVC uses. – NightWatchman Jul 15 '16 at 23:59
3

I see the binding being registered by Ninject.Web.Common v3.2.3.0

If you are trying to mock the binding in your unit tests, you must remove it first like this:

// WebCommonNinjectModule loads HttpContextBase. We need to remove it
var httpContextBaseBinding = kernel.GetBindings(typeof(System.Web.HttpContextBase)).FirstOrDefault();
kernel.RemoveBinding(httpContextBaseBinding);
kernel.Bind<System.Web.HttpContextBase>().ToMethod(m => httpContextBaseMock.Object);
jsgoupil
  • 3,788
  • 3
  • 38
  • 53