Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use request scope in PortalModuleBase #4046

Closed
GerardSmit opened this issue Sep 3, 2020 · 2 comments · Fixed by #4053
Closed

Use request scope in PortalModuleBase #4046

GerardSmit opened this issue Sep 3, 2020 · 2 comments · Fixed by #4053

Comments

@GerardSmit
Copy link
Contributor

GerardSmit commented Sep 3, 2020

Description of problem

In the class PortalModuleBase it is possible to resolve services through the property DependencyProvider.

In PR #3930 the implementation changed which causes every control to have its own scope:

private readonly Lazy<IServiceScope> _serviceScope = new Lazy<IServiceScope>(Globals.DependencyProvider.CreateScope);

Which is an improvement of the previous implementation (using the global scope) however it comes with a drawback:

Let's say I've registered a scoped service that uses a database connection and stores cache in a field.

  • When I resolve the service in control A, the instance is created and the data is cached.
  • When I resolve the same service in control B, it's creating a new instance because it's a different scope. The service retrieves the same data again.

Description of solution

In #3520 @dimarobert created HttpContext.Current.GetScope(). I would suggest to use the same scope in PortalModuleBase and if the HTTP Context doesn't exists, then it would create a new scope.

Additional context

In ASP.Net Core, scoped services are created per request (see https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection#request-services). So I assumed this was also the case in DotNetNuke.

Workaround

It's possible to use Context.GetScope().ServiceProvider instead of DependencyProvider to use the request scope.

@bdukes
Copy link
Contributor

bdukes commented Sep 4, 2020

Agreed, we need to be using request scope where it's available. Would you like to submit a PR with that change?

@GerardSmit
Copy link
Contributor Author

I've created a PR (#4053).

valadas pushed a commit that referenced this issue Sep 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants