Josh
Josh

Reputation: 8477

Setting AWS Services Lifetime in .NET Core

When adding AWS Services to Services Collection in .NET Core, should I go with the default which well add as a Singleton or should I use the override to set as Transient?

For reference, displaying default option (Singleton) for DynamoDB and Transient for SQS:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
        services.AddHttpContextAccessor();

        // Add AWS Services
        services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
        services.AddAWSService<IAmazonDynamoDB>();
        services.AddAWSService<IAmazonSQS>(lifetime: ServiceLifetime.Transient);
    }

I've seen many examples go with the default, but reading the is article suggests going with Transient unless there is a reason to go with Singleton: https://dotnetcoretutorials.com/2017/03/25/net-core-dependency-injection-lifetimes-explained/#comments

Upvotes: 0

Views: 1938

Answers (1)

Norm Johanson
Norm Johanson

Reputation: 3177

From a dev of the AWS SDK I recommend leaving it at the default. The AWS service clients added to the collection are thread safe. We added the overload to set the service lifetime to provide flexibility in case somebody is doing some really unusual.

Upvotes: 2

Related Questions