Copied to clipboard

Flag this post as spam?

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


  • Andrei 10 posts 92 karma points
    22 days ago
    Andrei
    0

    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 1540 posts 6087 karma points MVP 4x c-trib
    1 week ago
    Dan Diplo
    101

    You actually need to make it Lifetime.Scope as LifeTime.Request isn't what you think it it. Check the docs at https://our.umbraco.com/Documentation/Reference/Using-Ioc/

    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 10 posts 92 karma points
    1 week ago
    Andrei
    1

    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.

    Cheers

  • Dan Diplo 1540 posts 6087 karma points MVP 4x c-trib
    1 week ago
    Dan Diplo
    0

    Yeah, I made the same mistake, which is how I realised! I presume you might have read https://github.com/seesharper/LightInject/issues/494 ?

    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:

Draft