Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Andrei 15 posts 99 karma points
    Jul 09, 2021 @ 21:35

    LightInject with Lifetime.Request scope - service class created twice?

    I have a service class which I register with Lifetime.Request via a composer.

    I noticed that my service class gets instantiated two times when I'm using it in a NotFoundContentFinder.

    First I'm getting my service class here via a DependencyResolver.GetService<> call inside the NotFoundContentFinder. Then I'm injecting the same service class in a controller and through some logging I can see that my class is created two times during the lifetime of this request. This does not happen with a normal page where NotFoundContentFinder is not triggered.

    Any idea what happens here? Is somehow the Request object dumped and recreated when the not-found chain gets triggered?

    [Update] Did some further checking to see if the HttpRequest object gets replaced/renewed during request processing, but it doesn't seem to be the case. I checked that by adding a mark into the request items collection and it was still in place when the second service instance was created.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jul 20, 2021 @ 19:42
    Dan Diplo

    You actually need to make it Lifetime.Scope as LifeTime.Request isn't what you think it it. Check the docs at

    Lifetime.Request - always creates a new instance: A new instance will get created each time it is injected. Instances will get disposed at the end of a web request.

    Purely used to dispose objects created during a request at the end of the request, this does not mean they'll only be created once per web request.

    Lifetime.Scope - one unique instance per web request (or "scope"): Only creates one instance per web request. Instances will get disposed at the end of a web request.

  • Andrei 15 posts 99 karma points
    Jul 20, 2021 @ 21:29

    Ohh, that was a bit of a nasty blow. More so when I did read some related posts saying that lightinject has confusing naming, but somehow got the impression that umbraco masters have normalized these names to mean what they should. A bit surprised they chose to propagate the confusion.

    Kudos Dan, for clearing that up, thanks a lot.


  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jul 21, 2021 @ 09:06
    Dan Diplo

    Yeah, I made the same mistake, which is how I realised! I presume you might have read ?

    I guess in Umbraco 9 they will be using the more normalised .NET conventions!

Please Sign in or register to post replies

Write your reply to: