File tree Expand file tree Collapse file tree
EssentialCSharp.Web.Tests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -54,14 +54,20 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
5454 descriptor . ImplementationType == typeof ( DatabaseMigrationService ) ,
5555 nameof ( DatabaseMigrationService ) ) ;
5656
57- // Open a keep-alive connection to prevent the shared-cache in-memory database from
58- // being dropped when per-scope connections are disposed between requests.
59- // Register as scoped so each request scope gets its own SqliteConnection,
60- // preventing "database is locked" errors under concurrent requests.
57+ // Keep per-scope connections so each request/test scope uses a fresh DbConnection,
58+ // which avoids locking issues from sharing one SqliteConnection instance.
6159 services . AddScoped < DbConnection > ( _ =>
6260 {
6361 SqliteConnection conn = new ( _sqlConnectionString ) ;
64- conn . Open ( ) ;
62+ try
63+ {
64+ conn . Open ( ) ;
65+ }
66+ catch
67+ {
68+ conn . Dispose ( ) ;
69+ throw ;
70+ }
6571 return conn ;
6672 } ) ;
6773
@@ -73,7 +79,8 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
7379 options . UseSqlite ( dbConnection ) ;
7480 } ) ;
7581
76- // Ensure schema exists before any hosted service that reads from the database.
82+ // Ensure schema exists before hosted services by prepending this registration
83+ // at index 0 of the service collection.
7784 RemoveSingleOrNone (
7885 services ,
7986 descriptor => descriptor . ServiceType == typeof ( IHostedService ) &&
You can’t perform that action at this time.
0 commit comments