I'm using the DotNetCircuitBreaker and tries to call a async metod like this
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();
}
}
Argument 1: cannot convert from System.Threading.Tasks.Task"int" to System.Func"System.Threading.Tasks.Task"
How fix my calc method work with a
It looks like CircuitBreaker.ExecuteAsync
expects a parameter with type Func<Task<T>>
. What you supplied is a Task<T>
.
To fix it you can use a lambda expression like
var result = await circuitBreaker.ExecuteAsync(() => Calc(4, 4));
I agree with Dirk, since you are not currently adhering to the expected parameter signature. On a side note, have you considered looking into the circuit breaker provided by the Polly library ? It is quite mature and has a lot of functionality coming down the pike according to its roadmap , including some features that match or exceed the functionality provided by Netflix's Hystrix circuit breaker library (java).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.