您的位置:首页 > 其它

workflow学习笔记1,workflow运行时

2010-03-20 19:33 369 查看
当你在workflow环境中执行任务时,需要一些minitor来监视整个的代码执行过程,这个monitor命名为WorkflowRuntime,WorkflowRuntime会启动一个新的instance来完成上面的工作,在你的任务的执行的过程中,instance会根据外部的事件来执行对应的事件处理函数,需要指出的是这个WorkflowRuntime还能够指定一个service来保持跟踪,比如说添加一个SqlWorkflowPersistenceService服务,能够将正在执行的Workflow保存到sql server中,然后还可以将保存在数据库中的实例恢复。

Workflow应用程序通常的框架是:首先需要一个宿主程序,这个宿主程序可以是windows forms,控制台的应用程序,asp web程序,甚至可以是windows server,在宿主程序中添加Workflow工程的引用(具体的做法就是首先将该workflow工程编译完的.exe文件添加到宿主工程中),在苏中程序中:

_runtime = WorkflowFactory.GetWorkflowRuntime();

_instance = _runtime.CreateWorkflow(typeof(PersistedWorkflow.Workflow1));
PersistedWorkflow.Workflow1是workflow工程编译完的.exe添加引用之后的类似于命名空间的类型,使用上面的代码得到instance,然后该instance开始监控代码执行。

WorkflowRuntime常见属性和方法

AddService : 为workflow运行时添加指定的服务。能添加的服务类型和时间受到种种限制。。
CreateWorkflow : 创建一个workflow实例,它包含一些指定(但可选)的参数。假如workflow运行时没有启动,该方法就调用StartRuntime方法。
GetWorkflow : 通过 指明workflow实例的标识符(由一个Guid组成)来检索workflow实例。假如这个workflow 实例是空闲和持久化保存的,它将被重新加载并执行。
StartRuntime : 启动workflow 运行时和相关服务,并引发“Started”事件。
StopRuntime : 停止workflow 运行时和相关服务,并引发“Stoped”事件。

下面是一个WorkflowFactory的实现,使用单件的设计模式:

/*
* File : WorkflowFactory.cs
* Function : create Workflow and use singlenton, pay attention
* to that if you wnat to add service to the Workflow
* instance, add the service in this file, do not add
* the service in other file because after calling the
* GetWorkflowInstance(), the instance is runnning.
*/
using System;
using System.Workflow.Runtime;

using System.Workflow.Runtime.Hosting;
using System.Configuration;

namespace WorkflowPersister
{
public static class WorkflowFactory
{
private static WorkflowRuntime _workflowRuntime =
null;
private static object _syncRoot = new object();

public static WorkflowRuntime GetWorkflowRuntime ()
{
lock (_syncRoot)
{
if (null == _workflowRuntime)
{
AppDomain.CurrentDomain.ProcessExit +=
new EventHandler(StopWorkflowRuntime);
AppDomain.CurrentDomain.DomainUnload +=
new EventHandler(StopWorkflowRuntime);
_workflowRuntime = new WorkflowRuntime();

// Add service
string conn =
ConfigurationManager.ConnectionStrings["StorageDataBase"].ConnectionString;
_workflowRuntime.AddService(new SqlWorkflowPersistenceService(conn));

_workflowRuntime.StartRuntime();
}
}

return _workflowRuntime;
}

public static void StopWorkflowRuntime (object sender, EventArgs e)
{
if (_workflowRuntime != null)
{
if (_workflowRuntime.IsStarted)
{
try
{
_workflowRuntime.StartRuntime();
}
catch (ObjectDisposedException ex)
{
}
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: