HttpContext activation error. More than one mapping is available.

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); // <-- problem Bind<MainDbContext>().ToSelf().InRequestScope(); Bind<UserInfo>().ToSelf().InRequestScope(); } } 

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; } // ... etc ... // } 

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; } // ... actions here ... // } 

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?

+6
source share
2 answers

If you are using the Ninject.MVC3 extension, you must remove

 Bind<HttpContext>().ToMethod(context => HttpContext.Current); // <-- problem 

since the HttpContext binding is already added by the extension.

+5
source

You might want to quickly look at this file and see if it looks like your problem: "Multiple matching matches available" error when using Ninject.Web.Mvc 2.0 and ASP.NET MVC 1.0

+1
source

Source: https://habr.com/ru/post/898644/


All Articles