Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

C# Azure: How to set Azure timeout and retry policy when running locally?

I am writing C# code that runs against an Azure cloud. My application is an ASP.NET Core web service that exposes methods but no UI.

Sometimes I want to run my code locally using Microsoft Azure Storage Emulator. When my code starts up, one of the first things that happens is this:

var container = new BlobContainerClient(_connectionString, s);
bool exists = await container.ExistsAsync(ct);
if (!exists)
    await container.CreateAsync(cancellationToken: ct);

When running locally, I sometimes forget to start Azure Storage Emulator. When that happens, it takes my code like a minute to time out and tell me it can't reach the "cloud".

What I want to achieve is: Make the program give me good error messages quickly when running locally, but use more lenient timeout strategies when actually running in the cloud.

I can reduce the above timeout by doing something like this:

var blobClientOptions = new BlobClientOptions();
blobClientOptions.Retry.MaxRetries = 0;
var container = new BlobContainerClient(_connectionString, s, blobClientOptions);

... but when running against the real cloud I don't want that; I want it to retry. One option might be to set the retries to zero like above, but only when running locally.

I have a development-specific configuration file (appsettings.Development.json). Is it possible to configure such timeout/retry settings in the config file?

Or is there some other best-practice way to accomplish the "fail quickly in development" behaviour that I seek?

Thanks in advance!

question from:https://stackoverflow.com/questions/65950493/c-sharp-azure-how-to-set-azure-timeout-and-retry-policy-when-running-locally

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • create a class that will contain you blobstorage configuration:
public class BlobStorageConfiguration  
{
  public string ConnectionString {get; set;}
  public int MaxRetries {get; set;}
}
  • in your appsettings.Development.json
{
 ...
  "BlobStorageConfiguration": {
    "ConnectionString " : "<your_connection_string>",
    "MaxRetries ":0
  }
 ...
}

  • in your Startup.cs in the ConfigureServices method
..
 var blobConfig = new BlobStorageConfiguration ();
 Configuration.Bind(nameof(BlobStorageConfiguration ), blobConfig);
 services.AddSingleton(blobConfig );
..
  • now you can inject your config and it will take values from the appsettings.Development.json if you are running it locally:

some controller:

[Route("api/somthing")]
[ApiController]
    public class SomethingController : ControllerBase
        private readonly ILogger<SomethingController > logger;

        public SomethingController (
            ILogger<SomethingController > logger,
            BlobStorageConfiguration blobConfig)
        {
            this.logger = logger;
         // use your blobConfig (connectionstring and maxRetries)
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...