1

I am running an ASP.NET Core MVC app in a docker container, with an AWS credentials file. I have another service that is putting new keys into the file when the old ones expire, but these new keys don't seem to propagate through to my MVC app and my site crashes. I have seen that normally the solution to get strongly typed configuration to reload is to use IOptionsSnapshot, like:

services.AddDefaultAWSOptions(Configuration.GetAWSOptions())
        .AddScoped(config => config.GetService<IOptionsSnapshot<AWSOptions>>().Value)
        .AddAWSService<IAmazonS3>();

but this gives an exception:

System.InvalidOperationException: Cannot resolve scoped service 'Amazon.Extensions.NETCore.Setup.AWSOptions' from root provider.

Does anyone have a solution to getting ASP to reload the AWS credentials file? I'd like to continue using the AWS dependency injection extension if possible.

marcusturewicz
  • 2,394
  • 2
  • 23
  • 38

1 Answers1

2

By default, AddAWSService registers the client factory in singleton scope, which means it's one and done for the life of the application. However, AddAWSService has a lifetime param you can utilize to customize this. Essentially, you need a shorter lifetime on the client, so that it will be recreated with the new settings. You can choose either "scoped" (request-scoped) or "transient" (new instance every time it's injected).

Obviously with "scoped", you'll get a connection with the updated settings every request. However, if you do any further operations on the same request after the settings have been changed, it will remain the old connection with the old settings (i.e. you'll still have the same issue, at least for the life of the request).

Using "transient" scope, you'll have have a client with the most updated settings, but you'll end up basically with a client for every use, which may not be ideal.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • It seems that "scoped" is probably the best option and I'll just have to handle any errors and re-submit any requests that fail. Thanks! – marcusturewicz Jan 23 '18 at 02:17