这几天在研究Windows Workflow Foundation,天天上www.windowsworkflow.net 的论坛上查看和询问,全都是英文,头都看大了,对这金山词霸一个一个的啃。闲话少说,步入正题吧!

本篇主要讲WF在asp.net运用。如下:
我的流程设置如下:
WF in Asp.Net

设置了一个CodeActive和一个HandleExternalEventActive
CodeActive是执行一段代码的Active
HandleExternalEventActive是外联一个Event的Active

首先,配置web.config


简单介绍上面的配置文件
a.注册服务
这里是将一些服务注册到 Workruntime中,你也可以在代码中使用AddServcie中注册服务。
注册好服务后,就可以在代码中
ExternalDataExchangeService dataService = workflowRuntime.GetService<ExternalDataExchangeService>();
以上方式得到服务。
b.跟踪
配置跟踪信息,非常有用,我有一次调试的时候,MSDTC 服务没有启动的错误,就是查看WFTrace.log文件而获得的。

然后,在Global中启动一个WorkRunTime
WF in Asp.Netvoid Application_Start(object sender, EventArgs e)
WF in Asp.Net    {
WF in Asp.Net        System.Workflow.Runtime.WorkflowRuntime workflowRuntime = new System.Workflow.Runtime.WorkflowRuntime("WorkflowRuntime");
WF in Asp.Net        Application["WorkflowRuntime"] = workflowRuntime;
WF in Asp.Net
WF in Asp.Net        System.Workflow.ComponentModel.Compiler.TypeProvider typeProvider = new System.Workflow.ComponentModel.Compiler.TypeProvider(workflowRuntime);
WF in Asp.Net        typeProvider.AddAssembly(typeof(WorkflowLibrary1.Workflow1).Assembly);
WF in Asp.Net        workflowRuntime.AddService(typeProvider);
WF in Asp.Net
WF in Asp.Net        workflowRuntime.StartRuntime();
WF in Asp.Net    }
WF in Asp.Net
WF in Asp.Net    void Application_End(object sender, EventArgs e)
WF in Asp.Net    {
WF in Asp.Net        System.Workflow.Runtime.WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime;
WF in Asp.Net        workflowRuntime.StopRuntime();
WF in Asp.Net    }

接着,就可以在程序中运行workflow了
WF in Asp.Netprivate void StartWorkflow()
WF in Asp.Net    {
WF in Asp.Net        WorkflowRuntime workflowRuntime = StartRuntime();
WF in Asp.Net
WF in Asp.Net        // Now get a refernece to the ManualWorkflowSchedulerService
WF in Asp.Net        ManualWorkflowSchedulerService scheduler =
WF in Asp.Net            workflowRuntime.GetService
<ManualWorkflowSchedulerService>();
WF in Asp.Net
WF in Asp.Net        // Attach to the WorkflowCompleted event
WF in Asp.Net        workflowRuntime.WorkflowCompleted += new EventHandler
<WorkflowCompletedEventArgs>(WorkflowRuntime_WorkflowCompleted);
WF in Asp.Net
WF in Asp.Net        WorkflowInstance workflowInstance = workflowRuntime.CreateWorkflow(typeof(WorkflowLibrary1.Workflow1));
WF in Asp.Net
WF in Asp.Net        workflowInstance.Start();
WF in Asp.Net
WF in Asp.Net        // Now run the workflow.  This is necessary when 
WF in Asp.Net        // WF in Asp.Netusing the ManualWorkflowSchedulerService
WF in Asp.Net        scheduler.RunWorkflow(workflowInstance.InstanceId);
WF in Asp.Net    }
WF in Asp.Net
WF in Asp.Net
WF in Asp.Net
WF in Asp.Net
WF in Asp.Net    private WorkflowRuntime StartRuntime()
WF in Asp.Net    {
WF in Asp.Net        WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime;
WF in Asp.Net
WF in Asp.Net        ExternalDataExchangeService dataService = workflowRuntime.GetService
<ExternalDataExchangeService>();
WF in Asp.Net        OrderLocalServices.OrderService orderService = workflowRuntime.GetService
<OrderLocalServices.OrderService>();
WF in Asp.Net
WF in Asp.Net        if (orderService == null)
WF in Asp.Net        {
WF in Asp.Net            orderService = new OrderLocalServices.OrderService();
WF in Asp.Net            dataService.AddService(orderService);
WF in Asp.Net        }
WF in Asp.Net
WF in Asp.Net        // Attach to the WorkflowCompleted event
WF in Asp.Net        workflowRuntime.WorkflowCompleted += new EventHandler
<WorkflowCompletedEventArgs>(WorkflowRuntime_WorkflowCompleted);
WF in Asp.Net        return workflowRuntime;
WF in Asp.Net    }

最后,运行代码后,当运行完上图中的CodeActive后,该workflowinstance将进入等待状态(会触发workflowRuntinme的WorkflowIdled事件)。
这个时候你如果注册了SqlWorkflowPersistenceService服务,和创建了PersistenceStore数据库,则会将此
工作流保存到数据库中,已备下次使用,关于这块下次我再整理。
为什么工作流会等待了,
因为接下来的环节HandleExternalEventActive的Active必须需要触发了和他相关联的事件后,他才会继续的。
所以,我可以针对workflowRuntinme增加WorkflowIdled事件,实现事件的代码如下:
WF in Asp.Net   void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)
    }

这样这个工作流就整个的执行完成了。






相关文章: