似乎没有任何AdvancedCircuitBreakerAsync
的过载将Context
传递到onHalfOpen
行动,所以我想知道是否有任何解决方案。
var cB = Policy
.Handle<Exception>()
.AdvancedCircuitBreakerAsync(
getFailureThreshold(),
getSamplingDuration(),
getMinimumThroughput(),
getDurationOfBreak(),
onBreak: (exception, timeSpan, context) =>
{
var logger = context.GetLogger();
logger.LogWarning("In Break");
},
onReset: (context) =>
{
var logger = context.GetLogger();
Logger.LogWarning("In Reset");
},
onHalfOpen: () =>
{
// How do I get the context here?
});
可悲的事实是你不能。如果您查看相关的源代码,那么您可以看到所有可以接受onHalfOpen
委托的CircuitBreakerAsync
重载都以onHalfOpen
是Action
的方式定义:
public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, TimeSpan> onBreak, Action onReset, Action onHalfOpen)
public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen)
public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, CircuitState, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen)
遗憾的是,AdvancedCircuitBreakerAsync
也是如此
public static CircuitBreakerPolicy<TResult> AdvancedCircuitBreaker<TResult>(this PolicyBuilder<TResult> policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action<DelegateResult<TResult>, TimeSpan> onBreak, Action onReset, Action onHalfOpen)
public static CircuitBreakerPolicy<TResult> AdvancedCircuitBreaker<TResult>(this PolicyBuilder<TResult> policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action<DelegateResult<TResult>, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen)
public static CircuitBreakerPolicy<TResult> AdvancedCircuitBreaker<TResult>(this PolicyBuilder<TResult> policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action<DelegateResult<TResult>, CircuitState, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen)
有人在2021年提出了同样的问题。请求更新可能是有意义的。
这里有一些同样关注的封闭问题:1,2,…