I have an ASP.NET MVC application with a simple NinjectModule:
public class MainModule : NinjectModule { public override void Load() { Bind<AppSettings>().ToSelf().InSingletonScope(); Bind<HttpContext>().ToMethod(context => HttpContext.Current);
This is the only required code in my application. When I run my application, I immediately get this runtime error:
HttpContext activation error
More than one mapping is available.
Activation path:
3) Injection of the HttpContext dependency into the httpContext parameter of the constructor of the UserInfo type
2) Injection of the UserInfo dependency in the userInfo parameter of the constructor of type HomeController
1) Inquiry for HomeController
Suggestions:
1) Make sure you define the binding for the HttpContext only once.
The error message seems to indicate that I bound the HttpContext
binding more than once, but the only mandatory statements in the entire application are in the MainModule
, and I clearly defined only one binding for the HttpContext
. If I comment on this line of code, I stop getting the error, but the HttpContext
that is being entered incorrectly is an empty, recently created HttpContext
, not HttpContext.Current
).
The error message describes the exact injection sequence I would expect ...
HttpContext
should be introduced into the UserInfo
constructor, which looks like this:
public class UserInfo { private readonly HttpContext _httpContext; public UserInfo(HttpContext httpContext) { _httpContext = httpContext; }
And UserInfo
should be introduced into the constructor of the HomeController
, which looks like this:
public class HomeController : Controller { private readonly AppSettings _appSettings; private readonly UserInfo _userInfo; public HomeController(AppSettings appSettings, UserInfo userInfo) { _appSettings = appSettings; _userInfo = userInfo; ViewData[Token.AppSettings] = _appSettings; ViewData[Token.UserInfo] = _userInfo; }
Why does this lead to an error? This seems like a very simple dependency injection scenario. How do I define a binding for an HttpContext
more than once?