我在 PolicyRegistry 中有以下策略可以在全球范围内重用:
var fallbackPolicy = Policy
.Handle<DrmException>().OrInner<DrmException>()
.Fallback(
fallbackAction: () => { //should commit or dispose the transaction here using a passed in Func or Action },
onFallback: (exception) => { Log.Error().Exception(exception).Message($"Exception occurred, message: {exception.Message}.").Write(); }
);
我有以下要实现的代码fallbackPolicy
:
if(Settings.DRM_ENABLED)
drmManager.ExecuteAsync(new DeleteUser(123).Wait();//HTTP Call, throws DrmException if unsuccessful
//in some cases, there is an if(transaction == null) here (if transaction was passed as a parameter and needs to be committed here)
transaction.Commit();//if not thrown - commits the transaction
我希望它看起来像这样:
var fallbackPolicy = Policy
.Handle<DrmException>().OrInner<DrmException>()
.Fallback(
fallbackAction: (transaction) => { transaction.Dispose(); },
onFallback: (exception) => { Log.Error().Exception(exception).Message($"Exception occurred, message: {exception.Message}.").Write(); }
);
fallbackPolicy.Execute(() => drmManager.ExecuteAsync(new DeleteUser(123).Wait(), transaction)
据我了解,fallbackPolicy.Execute
需要执行 Action/Func ,要么成功,在这种情况下fallbackPolicy
没有命中,要么失败,在这种情况下fallbackPolicy
,使用一些预定义的fallbackAction
.
我想做的是在执行策略时传入两个处理程序(onFail(transaction)
处理事务和onSuccess(transaction)
提交事务)。有没有更简单的方法来代替包装或使用 Polly 的上下文?