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.

DbContextFactory created with Scoped lifetime instead of Singleton

See original GitHub issue

I have seen previous issues about DbContextFactory and the suggestion was to use AddTriggeredDbContextFactory to solve those problems.

However, when we tried to do this it caused a bunch of other issues throughout the codebase because AddTriggeredDbContextFactory seems to cause the context factory to be changed to a scoped lifetime somehow.

We found that using services.AddDbContextFactory<TDbContext>(builder => { ...}, ServiceLifetime.Transient); solved the issues for us.

I am interested to know if you see any potential issues with using the context factory with a transient service lifetime like this? Our automated tests and passing and some manual tests show that the triggers are working correctly as expected but I worry that there might be some unexpected consequences that will bite us later down the line.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
cdimitroulascommented, Feb 23, 2022

I opened a very basic PR, I didn’t spend much time on it so please let me know if there are more changes required

1reaction
koenbeukcommented, Feb 27, 2022

This library internally uses a scoped IDbContextFactory implementation

This is the current effect of calling AddTriggeredDbContextPool, AddTriggeredDbContextFactory and AddTriggeredPooledDbContextFactory and this should be fixed.

This library needs to grab the current ServiceProvider that is used to either get an instance of IDbContextFactory or otherwise a Lease on an instance of a DbContext in the pool (each instance essentially acts as a singleton in the latter case). By storing this ServiceProvider, this library can then associate that ServiceProvider instance with the DbContext so that triggers can resolve services using the same ServiceProvider.

There is no need for this custom IDbContextFactory implementation to be registered as Scoped. Instead, Transient will work in both worlds. We should update these registrations to use a Transient registration instead.

I’m still unclear on how this could be resolved without requiring developers to change to the IServiceScopeFactory pattern.

Once we update the registrations to Transient, we will be able to resolve a IDbContextFactory from the root ServiceProvider, as this is able to resolve Singleton and Transient services. Any trigger however that relies on a Scoped service will still cause issues as that root ServiceProvider is unable to deal with them. It therefore is strongly recommended to IServiceScope pattern as you highlighted above in singleton services.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use DbContext in ASP .Net Singleton Injected Class
See this answer. An object cannot have dependencies with a shorter lifetime than itself. You can either inject a factory to create shorter...
Read more >
DbContext Lifetime, Configuration, and Initialization
The lifetime of a DbContext begins when the instance is created and ends when the instance is disposed. A DbContext instance is designed...
Read more >
When Your DbContext Has The Wrong Scope - Haacked
Singleton . As a Singleton, it can't consume a Scoped service because the Scoped service has a shorter lifetime than the Singleton service....
Read more >
Register a scoped DbContext automatically when using ...
AddDbContext registers DbContextOptions as scoped by default. This cannot be consumed from the default singleton factory. Changing the lifetime ...
Read more >
Best Practices in Using the DbContext in EF Core
The DbContext is a singleton class that represents the gateway to all data access, and therefore should not be instantiated more than once....
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