How do I pass context to a circuit breaker to be used onbreak or onreset?
Hi,
This example in the docs is confusing me a little...
// Break the circuit after the specified number of exceptions
// and keep circuit broken for the specified duration,
// calling an action on change of circuit state,
// passing a context provided to Execute().
Action<Exception, TimeSpan, Context> onBreak = (exception, timespan, context) => { ... };
Action
I can see how to get context when onBreak or onReset is called, but not how to generate this context on Execution so that it is made available when onBreak fires.
I have created a static instance of the circuit breaker policy so that the breaker can correctly keep track of the counts, but then invoke the circuit breaker wrapper with the ExecuteAndCaptureAsync method. I can't see any overload to pass context at this point... (nor can I see this on any of the other policy method such as ExecuteAsync)
When I look at the retry policy WaitAndRetryAsync it's execute method has an overload that takes a Function and a dictionary object, which is then presumably made available as context.
Am I missing something?
Thanks
Never mind. Worked out that I need to store a CircuitBreakerPolicy not a Policy object as my static pointer to the circuit breaker policy...
Hi @bunceg . Re:
Worked out that I need to store a CircuitBreakerPolicy not a Policy object as my static pointer to the circuit breaker policy
Yes, that would be needed. We had to retain the ability for people to code Policy myPolicy = ....CircuitBreaker(...) for backwards compatibility. But, to gain access to the V4 circuit-breaker's full features (including passing context), you have to switch to var myPolicy = ....CircuitBreaker(...) or CircuitBreakerPolicy myPolicy = ....CircuitBreaker(...)
Hope this helps!