用于函数重试的 C# Polly WaitAndRetry 策略
C# Polly WaitAndRetry policy for function retry
我是 C# 编码的新手,我只想知道如何在函数失败时为我的函数设置 polly WaitAndRetry。以下是我的步骤
- 我使用 NuGet 包安装了包 Install-Package Polly
- 在我的代码中添加了using polly。
- 下面是我的代码
public async Task<string> ConfigInsert(config model)
{
try
{
SendToDatabase(model);
await Policy.Handle<Exception>()
.RetryAsync(NUMBER_OF_RETRIES)
.ExecuteAsync(async () =>
await SendToDatabase(model))
.ConfigureAwait(false);
}
Catch(Exception e)
{
_log.write("error occurred");
}
public async Task<string> SendToDataBase(config model)
{
var ss = DataBase.PostCallAsync(model)
.GetAwaiter()
.GetResult();
return ss;
}
}
但是这个电话一直在打,没有任何延迟。我尝试在 catch 调用中使用 WaitAndRetryAsync,但它不起作用。 WaitAndRetryAsync 仅接受 HTTP repose 消息。我想在 try-catch
中实现 ait 和 retry 选项
您说您想要 WaitAndRetry 但您没有使用该功能...而且它不仅适用于 HttpResponse。请阅读 documentation.
下面的代码应该可以让您先行一步:
class Program
{
static async Task Main(string[] args)
{
// define the policy using WaitAndRetry (try 3 times, waiting an increasing numer of seconds if exception is thrown)
var policy = Policy
.Handle<Exception>()
.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)
});
// execute the policy
await policy.ExecuteAsync(async () => await SendToDatabase());
}
static async Task SendToDatabase()
{
Console.WriteLine("trying to send to database");
await Task.Delay(100);
throw new Exception("it failed!");
}
}
class Program
{
static Main(string[] args)
{
// define the policy using WaitAndRetry (try 3 times, waiting an increasing numer of seconds if exception is thrown)
var policy = Policy
.Handle<Exception>()
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)
});
// execute the policy
policy.Execute(() => SendToDatabase());
}
}
我是 C# 编码的新手,我只想知道如何在函数失败时为我的函数设置 polly WaitAndRetry。以下是我的步骤
- 我使用 NuGet 包安装了包 Install-Package Polly
- 在我的代码中添加了using polly。
- 下面是我的代码
public async Task<string> ConfigInsert(config model)
{
try
{
SendToDatabase(model);
await Policy.Handle<Exception>()
.RetryAsync(NUMBER_OF_RETRIES)
.ExecuteAsync(async () =>
await SendToDatabase(model))
.ConfigureAwait(false);
}
Catch(Exception e)
{
_log.write("error occurred");
}
public async Task<string> SendToDataBase(config model)
{
var ss = DataBase.PostCallAsync(model)
.GetAwaiter()
.GetResult();
return ss;
}
}
但是这个电话一直在打,没有任何延迟。我尝试在 catch 调用中使用 WaitAndRetryAsync,但它不起作用。 WaitAndRetryAsync 仅接受 HTTP repose 消息。我想在 try-catch
中实现 ait 和 retry 选项您说您想要 WaitAndRetry 但您没有使用该功能...而且它不仅适用于 HttpResponse。请阅读 documentation.
下面的代码应该可以让您先行一步:
class Program
{
static async Task Main(string[] args)
{
// define the policy using WaitAndRetry (try 3 times, waiting an increasing numer of seconds if exception is thrown)
var policy = Policy
.Handle<Exception>()
.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)
});
// execute the policy
await policy.ExecuteAsync(async () => await SendToDatabase());
}
static async Task SendToDatabase()
{
Console.WriteLine("trying to send to database");
await Task.Delay(100);
throw new Exception("it failed!");
}
}
class Program
{
static Main(string[] args)
{
// define the policy using WaitAndRetry (try 3 times, waiting an increasing numer of seconds if exception is thrown)
var policy = Policy
.Handle<Exception>()
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)
});
// execute the policy
policy.Execute(() => SendToDatabase());
}
}