在ASP.net中使用State Machine Workflow

在ASP.net里应用Workflow和客户端程序中差别还是很大的,但这本Pro WF书里对这部分一笔带过,给了个顺序工作流的例子,但看完了还是没什么头绪,后天来找到了一个例子,才弄明白了怎么做。

首先,在ASP.net的Web应用里,因为和用户交互是单线程的,异步的执行是没法把结果反馈给用户的,所以需要以同步(Synchronized)的方式执行工作流,这就需要创建一个ManualWorkflowSchedulerService得实例schedule,注册到WorkflowRuntime上,来覆盖了默认的DefaultWorkflowSchedulerService。在创建了Workfow Instance并调用了instance.Start()的时候,工作流还并没有开始运行,但已经On schedule了,这时候再调用schedule.RunWorkflow(instance.InstanceId),这时工作流就开始执行了,RunWorkflow会在工作流进入等待,或者结束的时候返回。

另外就剩下工作流运行环境初始化的问题了。这部分工作可以在Global.asax文件里完成,一个ASP.net应用里,WorkflowRuntime的实例只需要又一个,所以在各个页面里去初始化就太浪费了,在Application_Start和Application_End里完成WorkflowRuntime的初始化和清理工作:

void Application_Start(object sender, EventArgs e)
{
	System.Workflow.Runtime.WorkflowRuntime workflowRuntime = new System.Workflow.Runtime.WorkflowRuntime("WorkflowRuntime");
	System.Workflow.ComponentModel.Compiler.TypeProvider typeProvider = new System.Workflow.ComponentModel.Compiler.TypeProvider(workflowRuntime);
	typeProvider.AddAssembly(typeof(Ganji.OA.Workflow.ExpensesClaim).Assembly);
	workflowRuntime.AddService(typeProvider);
	workflowRuntime.StartRuntime();
	Application["WorkflowRuntime"] = workflowRuntime;
}

void Application_End(object sender, EventArgs e)
{
	System.Workflow.Runtime.WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime;
	workflowRuntime.StopRuntime();
}

That’s all

This entry was posted in 计算机技术 and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>