使用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.mzph.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. 您是否收到有…