通过AEC解读WF的核心原理(二)通过AEC调用添加到引擎中的服务对像
说明
TrackingService,PersistenceService,ExternalDataExchange等服务都是通过引擎的AddService方法添加的
可以在Activity中用ActivityExecutionContext的GetService方法得到WorkflowRuntime的AddService方法加载的外部对象
本例是一个将自定义对象通过引擎的AddService方法添加,自定义Activity在内部使用演示
例子下载:https://files.cnblogs.com/wxwinter/aec2.rar
例子
自定义功能类
public class wxdTest { public string Text { set; get; }
public void myTest(string v) { System.Console.WriteLine("一个外部实现:" + v); } } |
自定义Activity
public class Activity1 : Activity { public string Text { set; get; }
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { wxdTest obj = executionContext.GetService<wxdTest>(); obj.myTest(Text); this.Text = obj.Text; return base.Execute(executionContext); } } |
测试用工作流
public class Workflow1 : SequentialWorkflowActivity { private CodeActivity codeActivity1; private Activity1 activity11;
[System.Diagnostics.DebuggerNonUserCode] private void InitializeComponent() { this.CanModifyActivities = true; this.codeActivity1 = new System.Workflow.Activities.CodeActivity(); this.activity11 = new wxwinterAecTest.Activity1(); // // codeActivity1 // this.codeActivity1.Name = "codeActivity1"; this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity1_ExecuteCode); // // activity11 // this.activity11.Name = "activity11"; this.activity11.Text = "wxd"; // // Workflow1 // this.Activities.Add(this.activity11); this.Activities.Add(this.codeActivity1); this.Name = "Workflow1"; this.CanModifyActivities = false; }
public Workflow1() { InitializeComponent(); }
private void codeActivity1_ExecuteCode(object sender, EventArgs e) { System.Console.WriteLine("内部打印:" + activity11.Text); } } |
宿主
class Program { static void Main() { WorkflowRuntime workflowRuntime = new WorkflowRuntime(); wxdTest obj = new wxdTest(); obj.Text = "lzm";
workflowRuntime.AddService(obj);
workflowRuntime.WorkflowCompleted +=new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted); workflowRuntime.WorkflowTerminated +=new EventHandler<WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated); workflowRuntime.WorkflowIdled+=new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled); WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1)); instance.Start();
System.Console.Read(); }
static void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e) { System.Console.WriteLine("WorkflowIdled"); }
static void workflowRuntime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e) { System.Console.WriteLine("Terminated" + e.Exception.Message); }
static void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e) { System.Console.WriteLine("WorkflowCompleted"); }
} |
运行结果
总结:
AEC为Activity访问加载到引擎中的服务提供了接口
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)