在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"] 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,可以得到计算的结果,
如下图:
 

你可能感兴趣的:(.net,mvc,workflow,asp.net,asp)