Retrieving dependencies in Application_Start when using LightInject.Mvc
See original GitHub issueI’m using LightInject.MVC in an MVC 4 Application. The first block of my Application_Start method creates and intializes my ServiceContainer with a set of service registrations. It also sets the scope manager to PerRequestScope.
var container = new ServiceContainer();
container.EnablePerWebRequestScope();
container.EnableAnnotatedConstructorInjection();
container
.ScanAndLoadAllCompositionRoots() //Registrations are performed here
container.RegisterControllers(Assembly.GetCallingAssembly(),Assembly.GetExecutingAssembly());
container.EnableMvc();
When I try to resolve one of my services later on in the Application_Start method as so:
var authorizationPermissionManager = container.GetInstance<IAuthorizationPermissionManager>();
I get the following exception:
“Unable to locate a scope manager for the current HttpRequest. Ensure that the LightInjectHttpModule has been added.”
I understand that the scope manager isn’t associated with the HttpRequest until after Application_Start is executed, so I’m confused on how I’m supposed to retrieve my dependency.
Issue Analytics
- State:
- Created 8 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Mvc
Mvc provides an integration that enables dependency injection in ASP.NET MVC applications. Installing¶. LightInject.Mvc provides two distribution models via ...
Read more >light inject - LightInject issue with DependencyResolver
I've wrote the config for lightinject into a separate class. In Global.ascx Application Start I have AreaRegistration.RegisterAllAreas(); ...
Read more >Web Api
WebApi provides an integration that enables dependency injection in Web API applications. Installing¶. LightInject.WebApi provides two distribution models via ...
Read more >LightInject
The we register the dependency (IBar) with the PerScopeLifetime that is going to cause the container to ask for the current scope so...
Read more >Dependency Injection in ASP.NET Web API
Please Note, for time being I am only converting the first action method 'LoadProducts' to use Ninject. This method brings all the product...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That is very plausible. That is why you might want to do this before enabling the perwebrequestscope.
Hi again. Took a look at your repo and it looks like it only contains parts of the original application.
What I meant by this is a minimal and runnable application that contains no more than the code needed to demonstrate the problem. A small reproduction that we can fork and hit F5 to see the problem.
This might seem like nitpicking to some people, but otherwise it would take to much time to try to figure out the context and the surrounding parts of you applications.