-
Notifications
You must be signed in to change notification settings - Fork 491
Description
Issue
When running the lambda function deployed on AWS the resources added as a scope ServiceCollection.AddScoped<...>
are not being disposed when the execution ends. It is basically working as a AddSingleton
and the resource is being reused.
If you run the service locally on visual studio you do not find this issue.
Scenario
In my scenario I made a one transaction per request using DI on my Startup.cs
.
serviceCollection.AddScoped<DbConnection>((serviceProvider) =>
{
var dbConnection = new SqlConnection(connectionString);
dbConnection.Open();
return dbConnection;
});
serviceCollection.AddScoped<DbTransaction>((serviceProvider) =>
{
var dbConnection = serviceProvider
.GetService<DbConnection>();
return dbConnection.BeginTransaction(level);
});
...
At the end of my execution the resources Added as scoped should be disposed automatically. Also, I'm commiting my transaction.
If you run the lambda function again instead of create a new scope, it will reuse the ones your created. In this case I'm getting Transaction has already being commited
. If I close and dispose the connection manually, then I get Connection is closed
.
Expected Result
Resources in AddScoped should be disposed when the execution ends. That way, every new execution will create a new connection and transaction.