简体   繁体   English

C# Azure:本地运行时如何设置Azure超时和重试策略?

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

I am writing C# code that runs against an Azure cloud.我正在编写针对 Azure 云运行的 C# 代码。 My application is an ASP.NET Core web service that exposes methods but no UI.我的应用程序是一个 ASP.NET 核心 web 服务,它公开方法但没有 UI。

Sometimes I want to run my code locally using Microsoft Azure Storage Emulator.有时我想使用 Microsoft Azure 存储模拟器在本地运行我的代码。 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.在本地运行时,有时会忘记启动 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 ).我有一个特定于开发的配置文件( 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!提前致谢!

  • create a class that will contain you blobstorage configuration:创建一个包含 blobstorage 配置的 class :
public class BlobStorageConfiguration  
{
  public string ConnectionString {get; set;}
  public int MaxRetries {get; set;}
}
  • in your appsettings.Development.json在你的appsettings.Development.json
{
 ...
  "BlobStorageConfiguration": {
    "ConnectionString " : "<your_connection_string>",
    "MaxRetries ":0
  }
 ...
}

  • in your Startup.cs in the ConfigureServices methodConfigureServices方法中的Startup.cs
..
 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:现在您可以注入您的配置,如果您在本地运行它,它将从appsettings.Development.json中获取值:

some controller:一些 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)
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何影响Azure C#中GetSubscription()和SubscriptionExists()的重试策略 - How do I affect a retry policy for GetSubscription() and SubscriptionExists() in Azure C# 如何使用 Azure.Storage.Blobs 程序集对 Azure blob 存储操作设置重试策略? - How do I set a retry policy on an Azure blob storage operation using the Azure.Storage.Blobs assembly? C# Azure ServiceBus / Azure SQL - 在本地工作,部署到 Azure 时似乎不起作用 - C# Azure ServiceBus / Azure SQL - works locally, does not seem to work when deployed to Azure 如何在本地选择默认文化 - (以及在 Azure 上运行时为什么不选择) - How is a default culture selected locally - (and why not when running on Azure) 如何在本地加速调试 C# Azure 函数? 是 - How to speed up debugging C# Azure Function locally? Is C#文件上传在本地工作; 不在Azure上 - C# File Upload works locally; not on Azure Azure Vault 在本地运行 - Azure Vault Running Locally 如何配置运行**本地**的 Azure Function 以使用 Azure B2C 执行身份验证? - How can I configure an Azure Function running **locally** to use Azure B2C to perform the authentication? 实体框架 - SQL Azure重试策略 - Entity Framework - SQL Azure Retry Policy 使用Azure Redis缓存重试策略 - Retry policy using Azure Redis Cache
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM