Skip to content

ServiceCollection.AddScoped Method does not work as expected on AWS. #677

@Edulopez

Description

@Edulopez

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions