question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Retrieving dependencies in Application_Start when using LightInject.Mvc

See original GitHub issue

I’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:open
  • Created 8 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
seesharpercommented, Mar 30, 2015

That is very plausible. That is why you might want to do this before enabling the perwebrequestscope.

0reactions
seesharpercommented, Oct 10, 2017

Hi again. Took a look at your repo and it looks like it only contains parts of the original application.

“If you put thus into a simple repro, we can have a look at it.”

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found