Polly每次重試執行不同的操作
前言
??最近在寫WG(用的 .net core 3.1 + wpf + abp vnext),程式里大量用到了重試機制選用的是Polly組件(只知道這個...),
遇到的問題
??在當前螢屏下根據NPC名稱查找NPC的坐標,但自己的人物可能會擋住NPC,導致識別不到
需求
??重試的時候希望執行
- 移動操作
- 根據重試次數調整文字識別準確度引數,
尋求解決辦法歷程
-
百度、Bing!關鍵字搜索
Polly等,說實話,官方更新的速度太快了,很多教程都已經不全面了,未果 -
官方檔案!是在是太多了,最開始只看與
Execute關鍵詞有關的資訊,沒找到,然后整體看了一遍,還是沒找到, -
Stackoverflow 各種關鍵詞未找到,
-
Google!
- 中文搜索!其實和Bing差不多,有時候還不如Bing,所以還是一樣,
- 英文搜索!其實是找到了一篇文章,但沒細看,最終錯過!
-
GitHub的Issues 最終找到了
https://github.com/App-vNext/Polly/issues/38,這種方法也想過,但覺得很不好,將要妥協的時候,在最后一樓發現The ability to do this is now delivered in Polly v5.1. Polly's flowing with the execution becomes mutable, to allow what @YoniH and @hawkunsh requested.Context See http://www.thepollyproject.org/2017/05/04/putting-the-context-into-polly/ for details. Shout if any questions/suggestions.
??也就是在4里發現的那篇文章,
最終代碼
Policy.Handle<Exception>()
.Retry(retry, (ex, retryCount, context) =>
{
//賦值 重試次數
context["retryCount"] = retryCount;
})
.Execute((ct) =>
{
//獲取 重試次數
var rc = ct["retryCount"];
Console.WriteLine(rc);
}, contextData: new Dictionary<string, object> { { "retryCount", 0 } /*初始化*/ });
結束
??百度|Bing|Google
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/54771.html
標籤:.NET Core
