0

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

4

1 回答 1

2

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.

于 2019-07-03T06:03:12.743 回答