本文是基于ASP.NET MVC的beta版本。
1.我们首先建立一个ASP.NET MVC的应用程序。在web.config中将下面的配置添加到相关位置,代码如下:
<?xml version="1.0"?> <configuration> <configSections> <section name="WorkflowRuntime" type="System.Workflow.Runtime.Configuration.
WorkflowRuntimeSection,System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/> </configSections> <WorkflowRuntime Name="WorkflowServiceContainer"> <Services> <add type="System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService,
System.Workflow.Runtime,Version=3.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/> <add type="System.Workflow.Runtime.Hosting.DefaultWorkflowCommitWorkBatchService,
System.Workflow.Runtime,Version=3.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/> <add type="System.Workflow.Runtime.Hosting.SqlWorkflowPersistenceService,
System.Workflow.Runtime,Version=3.0.00000.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"
UnloadOnIdle="true" LoadIntervalSeconds="5" ConnectionString="Initial Catalog=
WorkflowPersistence;Data Source=localhost\SQLEXPRESS;Integrated Security=SSPI;"/> </Services> </WorkflowRuntime> <appSettings/> <connectionStrings/> <system.web> <compilation debug="true"> <assemblies> <add assembly="Accessibility,Version=2.0.0.0,Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="System.Workflow.Runtime, Version=3.0.0.0,Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Workflow.ComponentModel, Version=3.0.0.0,Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Workflow.Activities, Version=3.0.0.0,Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/> </assemblies> </compilation> <authentication mode="Windows"/> </system.web> </configuration>
持久化服务在这个例子中你可以不必使用,但是真正的项目中是比不可少的。
2.然后在Global.asax.cs中的Application_Start()和Application_End分别启用和停止工作流引擎(WorkflowRuntime),
代码如下:
protected void Application_Start() { RegisterRoutes(RouteTable.Routes); WorkflowRuntime workflowRuntime =new WorkflowRuntime("WorkflowRuntime"); workflowRuntime.StartRuntime(); Application["WorkflowRuntime"] = workflowRuntime; } void Application_End(object sender, EventArgs e) { WorkflowRuntime workflowRuntime =Application["WorkflowRuntime"] as WorkflowRuntime; workflowRuntime.StopRuntime(); }
3.我们来设计我们的视图,我们来完成一个加法运算,Index视图的相关代码如下:
<p> <%Html.BeginForm("Compute","Home");%> <label>请输入第一个数字:</label><%=Html.TextBox("Number1") %><br /> <label>请输入第二个数字:</label><%=Html.TextBox("Number2") %><br /> <input type="submit" value="计算"></input><br/> <label>结果为:</label> <%=Html.Encode(ViewData["Result"]) %> <%Html.EndForm(); %> </p>
我们会在HomeControler的Compute Action中来调用WF来完成加法运算。
4.我们在看下HomeControler中的Compute Action,代码如下:
int Result = 0;
public ActionResult Compute() { ControllerContext cxt = this.ControllerContext; WorkflowRuntime workflowRuntime = cxt.HttpContext.Application["WorkflowRuntime"]
as WorkflowRuntime; ManualWorkflowSchedulerService scheduler =workflowRuntime.GetService( typeof(ManualWorkflowSchedulerService)) as ManualWorkflowSchedulerService; workflowRuntime.WorkflowCompleted+= new EventHandler<WorkflowCompletedEventArgs>( workflowRuntime_WorkflowCompleted); int Number1 = Int32.Parse(Request.Form["Number1"]); int Number2 = Int32.Parse(Request.Form["Number2"]); Dictionary<String, Object> wfPara= new Dictionary<string, object>(); wfPara.Add("Number1", Number1); wfPara.Add("Number2", Number2); WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(CaryWFLib.AddWorkflow),
wfPara); instance.Start(); scheduler.RunWorkflow(instance.InstanceId); ViewData["Result"]=Result; return View("Index"); } void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e) { if (e.OutputParameters.ContainsKey("Result")) { Result = (int)e.OutputParameters["Result"]; } }
注:
4.1.在ASP.NET MVC中通过如下代码来得到Global.asax.cs中的Application对象:
ControllerContext cxt = this.ControllerContext; WorkflowRuntime workflowRuntime = cxt.HttpContext.Application["WorkflowRuntime"] as WorkflowRuntime;
4.2.我们调用我们的工作流时,要装载ManualWorkflowSchedulerService服务,这点非常重要.这样可以让工作流同步的执行在
ASP.NET MVC的线程上。如果不装载该服务工作流实例会异步的执行在由Workflow runtime管理的线程上。
4.3.我们通过调用工作流来完成加法运算,并将得到的结果ViewData["Result"]返回给视图Index。
5.然后我们来看看我们的WF程序,我们只在工作流设计器中拖入一个CodeActivity,用它来完成我们加法运算的逻辑,工作流的代码如下:
public sealed partial class AddWorkflow: SequentialWorkflowActivity { public int Number1 { get; set; } public int Number2 { get; set; } public int Result { get; set; } public AddWorkflow() { InitializeComponent(); } private void codeActivity1_ExecuteCode(object sender, EventArgs e) { Result = Number1 + Number2; } }
6.整个项目完成后,项目结构如下图:
7.运行home/index后,我们输入两个数字,点击计算按钮会执行HomeControler的Compute Action,可以得到计算的结果,
如下图:
作者:生鱼片
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:
ASP.NET MVC
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器