我正在使用 DotNetCircuitBreaker 并尝试像这样调用异步方法
private async void button1_Click(object sender, EventArgs e)
{
var circuitBreaker = new CircuitBreaker.Net.CircuitBreaker(
TaskScheduler.Default,
maxFailures: 3,
invocationTimeout: TimeSpan.FromMilliseconds(100),
circuitResetTimeout: TimeSpan.FromMilliseconds(10000));
//This line gets the error... Definition looks like this
//async Task<T> ExecuteAsync<T>(Func<Task<T>> func)
var result = await circuitBreaker.ExecuteAsync(Calc(4, 4));
}
private async Task<int> Calc(int a, int b)
{
//Simulate external api that get stuck in some way and not throw a timeout exception
Task<int> calc = Task<int>.Factory.StartNew(() =>
{
int s;
s = a + b;
Random gen = new Random();
bool result = gen.Next(100) < 50 ? true : false;
if (result) Thread.Sleep(5000);
return s;
});
if (await Task.WhenAny(calc, Task.Delay(1000)) == calc)
{
return calc.Result;
}
else
{
throw new TimeoutException();
}
}
参数 1:无法从 System.Threading.Tasks.Task"int" 转换为 System.Func"System.Threading.Tasks.Task"
如何修复我的 calc 方法