在ASP.NET MVC中使用WF

简介:

本文是基于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"asWorkflowRuntime
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.整个项目完成后,项目结构如下图:
aspnetmvc2 
7.运行home/index后,我们输入两个数字,点击计算按钮会执行HomeControler的Compute Action,可以得到计算的结果,
如下图:

aspnetmvc1


本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2008/12/07/ASPNETMVC-WF.html,如需转载请自行联系原作者

目录
打赏
0
0
0
0
66
分享
相关文章
一款基于 .NET MVC 框架开发、功能全面的MES系统
一款基于 .NET MVC 框架开发、功能全面的MES系统
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
87 7
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
124 0
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
88 0
|
7月前
|
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
199 0
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
100 0
net MVC中的模型绑定、验证以及ModelState
net MVC中的模型绑定、验证以及ModelState 模型绑定 模型绑定应该很容易理解,就是传递过来的数据,创建对应的model并把数据赋予model的属性,这样model的字段就有值了。
1709 0
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
254 0
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
191 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等