workflow学习笔记1,workflow运行时

当你在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)
                    {
                    }
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值