Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Per Request / Scoped Dependency Injection #11600


User avatar
0
justinidsza created

Prerequisites What is your product version? 11.3

What is your product type (Angular or MVC)? Angular

What is product framework type (.net framework or .net core)? .NET Core

If issue related with ABP Framework What is ABP Framework version? 7.3

If issue is about UI Which theme are you using? Default What are the theme settings? Default

Hi,

The framework supports Transient and Singleton lifestyles for dependencies. I have a domain service whose lifestyle I would like to configure to be Per Request, as this domain service is heavily used and I want to reduce the number of unneccessary instantations of this domain service for each request.

I've tried this but it does not work. The dependency still behaves as Transient.

Please advise on how I can configure dependencies to be per request.

Thank you, Justin


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @justinidsza

    You can try using ASP.NET Core's scoped dependency injection, something similar to below;

    using var scope = _serviceProvider.CreateScope();
    var coreObjectManager = scope.ServiceProvider.GetRequiredService<CoreObjectsManager>();
    

    Does that work for you ?

  • User Avatar
    0
    justinidsza created

    Hi @ismcagdas,

    Thanks for the response.

    Unfortunately, I don't think that will work us. It looks like I would need to inject IServiceProvider into dependencies, which would be the Service Locator pattern. I consider that pattern to be an anti-pattern.

    I was under the impression that MsScopedLifestyleManager would work for ASP.NET Core, but If I look at the source code it seems that there is no active / current scope.

    I will investigate if I can implement a custom lifestyle manager based on HttpContext.

    Regards, Justin

  • User Avatar
    0
    ismcagdas created
    Support Team

    There is another interface named IPerWebRequestDependency which registers the class with the lifestyle MsScopedLifestyleManager. Maybe you can give it a try.