在 .NET Core 中设置 AWS 服务生命周期

Setting AWS Services Lifetime in .NET Core

在 .NET Core 中将 AWS 服务添加到服务集合时,我应该使用默认设置作为单例添加还是应该使用覆盖设置为瞬态?

作为参考,显示 DynamoDB 的默认选项 (Singleton) 和 SQS 的 Transient:

    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);
    }

我见过许多使用默认设置的示例,但阅读 is 文章建议使用 Transient,除非有理由使用 Singleton: https://dotnetcoretutorials.com/2017/03/25/net-core-dependency-injection-lifetimes-explained/#comments

来自 AWS SDK 的开发人员,我建议将其保留为默认值。添加到集合中的 AWS 服务客户端是线程安全的。我们添加了重载来设置服务生命周期,以提供灵活性,以防有人做一些非常不寻常的事情。