Windows Workflow Foundation (WWF) 提供流程驅動開發框架,可將商業流程 (如服務台工單、審核簽核) 以視覺化工作流定義,並宿主於 ASP.NET 應用程式中。本文探討 WWF 在 ASP.NET 中的 MVC 架構應用、狀態持久化與頁面導航分離。
傳統做法:頁面 A → 按鈕 → Response.Redirect("B.aspx"),流程硬編碼在頁面中。
WWF 做法:工作流活動完成 → 回傳下一步活動類型 → Controller 依據類型決定導向頁面。修改流程順序只需改工作流,頁面代碼完全不動。
// Global.asax Application_Start
WorkflowRuntime runtime = new WorkflowRuntime();
runtime.AddService(new SqlWorkflowPersistenceService(connStr));
runtime.StartRuntime();
Application["WorkflowRuntime"] = runtime;
// 建立工單頁面
Guid instanceId = Guid.NewGuid();
WorkflowInstance instance = runtime.CreateWorkflow(typeof(HelpDeskWorkflow), null, instanceId);
instance.Start();
// 將 instanceId 存入 Session 或資料庫,關聯使用者
Session["WorkflowInstanceId"] = instanceId;
Guid id = (Guid)Session["WorkflowInstanceId"];
WorkflowInstance instance = runtime.GetWorkflow(id);
// 將表單資料封裝為 Dictionary 傳給工作流
instance.EnqueueItem("SubmitQueue", formData, null, null);
SqlWorkflowPersistenceService 自動將閒置工作流序列化存入資料庫。// Controller 基類
public class WorkflowController : Page
{
protected void NavigateToNext(Type nextActivityType)
{
string pageUrl = PageMap.GetPageUrl(nextActivityType); // 映射表
Response.Redirect(pageUrl);
}
}