使用c#的 async/await編寫 長時間運行的基于代碼的工作流的 持久任務框架

持久任務框架?(DTF) 是基于async/await 工作流執行框架。工作流的解決方案很多,包括Windows Workflow Foundation,BizTalk,Logic Apps, Workflow-Core?和?Elsa-Core。最近我在Dapr 的倉庫里跟蹤工作流構建塊的進展時,深入了解了一下,這個DTFx在Azure 基礎設施有大量的應用,現在Dapr團隊正在把這個實踐抽象成工作流構建塊,具體參看https://github.com/dapr/dapr/issues/4576。DTFx 正好是.NET開發的,所以對他多了幾分關注,以前沒有深入進去看看,現在我覺得是值得推薦給大家的一個工作流方案,它足夠輕量級,而且非常簡單,依賴很少。

持久任務框架是一個開源框架,它為 .NET 平臺中的工作流即代碼提供了基礎。GitHub上:https://github.com/Azure/durabletask

它有兩個主要組件:業務流程和任務。業務流程“編排”應用程序邏輯,以內聯方式執行自定義代碼并調用任務。自定義業務流程派生自 TaskOrchestration<TResult, TInput>自定義任務派生自 TaskActivity<TInput, TResult>。

推薦大家從這兩個倉庫可用來學習和生產使用。

Microsoft.Extensions.Hosting包裝器:https://github.com/jviau/durabletask-hosting

持久任務框架擴展:https://github.com/lucaslorentz/durabletask-extensions

我們一起來看下持久任務框架的Hello world:代碼來自https://github.com/jviau/durabletask-hosting 的 DurableTask.Samples:

這個非常簡單的業務流程“GreetingsOrchestration”,有兩個稱為任務“GetUserTask”,它執行名稱提示和“SendGreetingTask”,它將問候語寫入控制臺。

GreetingsOrchestration 派生自 TaskOrchestration<string、string> 并具有調用 GetUserTask 和 SendGreetingTask 的 RunTask 方法。

using DurableTask.Core;

namespace DurableTask.Samples.Greetings;

/// <summary>
/// A task orchestration for greeting a user.
/// </summary>
public class GreetingsOrchestration : TaskOrchestration<string, string>
{
???? /// <inheritdoc />
???? public override async Task<string> RunTask(OrchestrationContext context, string input)
???? {
???????? string user = await context.ScheduleTask<string>(typeof(GetUserTask));
???????? string greeting = await context.ScheduleTask<string>(typeof(SendGreetingTask), user);
???????? return greeting;
???? }
}

GetUserTask 派生自 TaskActivity<string,string> 并實現了 Execute 方法

using DurableTask.Core;

namespace DurableTask.Samples.Greetings;

/// <summary>
/// A task activity for getting a username from console.
/// </summary>
public class GetUserTask : TaskActivity<string, string>
{
???? private readonly IConsole _console;

??? /// <summary>
???? /// Initializes a new instance of the <see cref="GetUserTask"/> class.
???? /// </summary>
???? /// <param name="console">The console output helper.</param>
???? public GetUserTask(IConsole console)
???? {
???????? _console = console ?? throw new ArgumentNullException(nameof(console));
???? }

??? /// <inheritdoc />
???? protected override string Execute(TaskContext context, string input)
???? {
???????? _console.WriteLine("Please enter your name:");
???????? return _console.ReadLine();
???? }
}

SendGreetingTask 派生自 TaskActivity<string、string> 并實現了 Excute 方法

using DurableTask.Core;

namespace DurableTask.Samples.Greetings;

/// <summary>
/// A task for sending a greeting.
/// </summary>
public sealed class SendGreetingTask : AsyncTaskActivity<string, string>
{
???? private readonly IConsole _console;

??? /// <summary>
???? /// Initializes a new instance of the <see cref="SendGreetingTask"/> class.
???? /// </summary>
???? /// <param name="console">The console output helper.</param>
???? public SendGreetingTask(IConsole console)
???? {
???????? _console = console ?? throw new ArgumentNullException(nameof(console));
???? }

??? /// <inheritdoc />
???? protected override async Task<string> ExecuteAsync(TaskContext context, string user)
???? {
???????? string message;
???????? if (!string.IsNullOrWhiteSpace(user) && user.Equals("TimedOut"))
???????? {
???????????? message = "GetUser Timed out!!!";
???????????? _console.WriteLine(message);
???????? }
???????? else
???????? {
???????????? _console.WriteLine("Sending greetings to user: " + user + "...");
???????????? await Task.Delay(5 * 1000);
???????????? message = "Greeting sent to " + user;
???????????? _console.WriteLine(message);
???????? }

??????? return message;
???? }
}

上面的這個例子非常基礎,我們在項目中要把它用起來就要用到這個擴展項目 https://github.com/lucaslorentz/durabletask-extensions。這個項目通過更多功能擴展持久任務框架,并使其更易于使用,目前還在開發過程中,尚未達到投入生產的程度。包含了下列這些功能,讓你在任何地方都可以運行。

  • 更多定義存儲功能的接口

  • 依賴注入集成

  • EF Core MySql/PostgreSQL/SqlServer storages

  • 分布式工作線程:允許在多個工作線程中拆分業務流程/活動實現

  • 通過 GRPC 協議進行間接存儲訪問:將您的存儲選擇和配置集中在單個組件中。

  • 用戶界面

  • BPMN 運行器

在示例文件夾中,您可以找到經典書籍《飛行、汽車、酒店》的實現,其中包含補償問題。

該示例旨在演示具有以下組件的微服務體系結構:

  • 服務器:連接到存儲并將其公開為 GRPC 終結點。

  • 應用程序接口:公開 REST API 以管理業務流程。

  • 用戶界面:公開用于管理業務流程的 UI。

  • 業務流程工作線程:為給定問題實現BookParallel和BookSquential業務流程。

  • 飛行工作人員:實施預訂航班和取消航班活動。

  • 車夫:實施“預訂汽車”和“取消汽車”活動。

  • 酒店工作人員:實施預訂酒店和取消酒店活動。

  • BPMNWorker:一個建立在持久任務之上的實驗性 BPMN 運行器。對于給定的問題,還有BookParallel和BookSequentialBPMN 工作流。

d7a52f772b9ca91d0631f6a2e29e8f3f.png

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/280847.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/280847.shtml
英文地址,請注明出處:http://en.pswp.cn/news/280847.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

bat批處理筆記

變量 1.CMD窗口變量&#xff0c;變量名必須用單%引用&#xff08;即&#xff1a;%variable&#xff09; 外部變量&#xff0c;是系統制定的&#xff0c;只有9個&#xff0c;專門保存外部參數的&#xff0c;就是運行批處理時加的參數。只有 %1 %2 %3 %4 ...... %9。 在bat內直…

多目標跟蹤(MOT)論文隨筆-SIMPLE ONLINE AND REALTIME TRACKING (SORT)

轉載請標明鏈接&#xff1a;http://www.cnblogs.com/yanwei-li/p/8643336.html 網上已有很多關于MOT的文章&#xff0c;此系列僅為個人閱讀隨筆&#xff0c;便于初學者的共同成長。若希望詳細了解&#xff0c;建議閱讀原文。 本文是使用 tracking by detection 方法進行多目標…

明日大盤走勢分析

如上周所述&#xff0c;大盤在4與9號雙線壓力下&#xff0c;上攻乏力。今天小幅下跌0.11%&#xff0c;漲511&#xff0c;平76&#xff0c;跌362&#xff0c;說明個股還是比較活躍&#xff0c;而且大盤上漲趨勢未加改變&#xff0c;只是目前攻堅&#xff0c;有點缺乏外部的助力。…

android EventBus 3.0 混淆配置

2019獨角獸企業重金招聘Python工程師標準>>> https://github.com/greenrobot/EventBus 使用的這個庫在github的官網README中沒有寫明相應混淆的配置. 經過對官網的查詢&#xff0c;在一個小角落還是被我找到了。 -keepattributes *Annotation* -keepclassmembers …

dotnet-exec 0.11.0 released

dotnet-exec 0.11.0 releasedIntrodotnet-exec 是一個 C# 程序的小工具&#xff0c;可以用來運行一些簡單的 C# 程序而無需創建項目文件&#xff0c;讓 C# 像 python/nodejs 一樣簡單&#xff0c;而且可以自定義項目的入口方法&#xff0c;支持但不限于 Main 方法。Install/Upd…

C# 讀取文件內容/輸出txt log

逐行讀 jsonString string.Empty;if (File.Exists(jsonFile)){StreamReader sr new StreamReader(jsonFile, Encoding.UTF8);string line string.Empty;while ((line sr.ReadLine()) ! null){jsonString line;}sr.Close();} 全讀取 string text File.ReadAllText("…

樹形dp-CF-337D. Book of Evil

題目鏈接&#xff1a; http://codeforces.com/problemset/problem/337/D 題目大意&#xff1a; 給一棵樹&#xff0c;m個點&#xff0c;一個距離d&#xff0c;求有多少個點A,使得A到所有的m個點距離都不超過d. 解題思路&#xff1a; 樹形dp. 有兩種方法可以解&#xff1a; 1、類…

運行時獲取類庫信息

運行時獲取類庫信息Intro在我們向別的開源項目提 issue 的時候&#xff0c;可能經常會遇到別人會讓我們提供使用的版本信息&#xff0c;如果別的開源項目類庫集成了 source link&#xff0c;我們可以從程序集信息中獲取到版本以及對應的 commit 信息&#xff0c;這樣我們就可以…

Oracle數據表中輸入引號等特殊字符

Oracle輸入特殊字符的特殊方法: UPDATE BOOKMARK SET BM_VALUEq/ --在這里寫下需要輸入的內容&#xff08;可以包括引號、回車等特殊的符號&#xff09;,所見即所得 / -- WHERE BM_NAMEXX

xbox360鏈接pc_如何將實時電視從Xbox One流式傳輸到Windows PC,iPhone或Android Phone

xbox360鏈接pcSet up your Xbox One’s TV integration and you can do more than just watch TV on your Xbox: you can also stream that live TV from your Xbox to a Windows 10 PC, Windows phone, iPhone, iPad, or Android device over your home network. 設置Xbox One…

PS2019工具介紹筆記(一)

通用快捷鍵 ALT鼠標滾輪放大縮小 空格按左鍵 移動圖片 一、新建 PPI 顯示器72PPI 印刷(國際通用分辨率)300PPI 海報高清寫真96-200PPI 大型噴繪25-50PPI 顏色模式 RGB(紅綠藍) CMYK(青洋紅黃黑)印刷業 二、移動工具 ctrlT 圖形自由變換 alt…

WPF ABP框架更新日志(最新2022-11月份)

更新說明本次更新內容包含了WPF客戶端以及Xamarin.Forms移動端項目, 更新內容總結如下:WPF 客戶端修復啟動屏幕無法跳轉異常修復添加好友異常修復托盤圖標狀態更新異常優化好友發送消息時狀態檢測更新聊天窗口UI風格更新好友列表得頭像顯示更新聊天窗口消息日期分組顯示更新系統…

JSONObject和JSONArray 以及Mybatis傳入Map類型參數

import org.json.JSONArray;import org.json.JSONObject;將字符串轉化為JSONArray JSONArray jsonArray new JSONArray(deviceInfo); //注意字符串的格式將JSONArray轉化為JSONObject類型 JSONObject jsonObject jsonArray.getJSONObject(0);將值存入Map Map<String,S…

十月cms_微軟十月更新失敗使整個PC行業陷入困境

十月cmsMicrosoft still hasn’t re-released Windows 10’s October 2018 Update. Now, PC manufacturers are shipping PCs with unsupported software, and Battlefield V is coming out next week with real-time ray-tracing technology that won’t work on NVIDIA’s RT…

ab 測試工具

ab&#xff0c;即Apache Benchmark&#xff0c;在Apache的安裝目錄中找到它。安裝目錄/bin/ab.exe。ab -n 數字 -c 數字 url路徑我們對位于本地Apache服務器上、URL為localhost/index.php的頁面進行壓力測試。測試總次數為1000&#xff0c;并發數為100(相當于100個用戶同時訪問…

bat批處理筆記(二)

eof 是“end of file”的縮寫 在批處理作用主要有二&#xff1a; 1、在無call的情況下&#xff0c;會直接退出批處理&#xff0c;此時等同于exit 2、在call的情況下&#xff0c;會中止call&#xff0c;繼續執行其他命令 echo off call :str1 pause goto :eof echo //此行代…

讓Visual Studio 2013為你自動生成XML反序列化的類

Visual Sutdio 2013增加了許多新功能&#xff0c;其中很多都直接提高了對代碼編輯的便利性。如&#xff1a; 1. 在代碼編輯界面的右側滾動條上顯示不同顏色的標簽&#xff0c;讓開發人員可以對所編輯文檔的修改、查找、定位情況一目了然。而不用像往常一樣上下不停地拖動滾動條…

20年的 .NET ,更需要 00 后的你

.NET 20 周年&#xff0c; 在國內有一大批和 .NET 一起成長的開發者&#xff0c;有一大批在不同行業采用 .NET 作為解決方案的企業。或者你會經常聽到很多的大神說他的 .NET 經歷&#xff0c;也會聽到 .NET “牛逼” 的故事&#xff0c;更會聽到用 .NET 不用“996”的神話。但對…

UIT創新科存儲系統服務“500強”汽車名企

信息化已成為汽車產業鏈各企業提高市場競爭力和傳統汽車產業謀求轉型升級的推動力&#xff0c;無論是汽車生產商&#xff0c;還是汽車服務商和零配件生產商&#xff0c;無不重視信息化系統的建設。某全球汽車行業著名的零配件生產商&#xff0c;財富500強企業之一&#xff0c;從…

通過從備份中排除這些文件夾來節省Time Machine驅動器上的空間

Are you getting notifications about a full Time Machine drive? Do you feel like your backups are taking too long? A bigger, faster hard drive might be the best solution, but you can also help by excluding particular folders from your backups. 您是否收到有…