工作流持久化助手

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Runtime.Hosting;

namespace NumeralSociety.WFInterface
{
    /// <summary>
    /// 工作流持久化助手
    /// </summary>
    public class WorkflowPersistenceUtility
    {
        /// <summary>
        /// 运行时
        /// </summary>
        WorkflowRuntime runtime;//运行时
        /// <summary>
        /// 实例
        /// </summary>
        WorkflowInstance instance;//实例
        /// <summary>
        /// 外部数据交换服务
        /// </summary>
        ExternalDataExchangeService service;//外部数据交换服务
        /// <summary>
        /// 持久化服务
        /// </summary>
        WorkflowPersistenceService perService;//持久化服务

        /// <summary>
        /// 实例化相关类,并加载
        /// </summary>
        /// <param name="PersistenceConnectionString"></param>
        /// <param name="achieveObject"></param>
        public void mInstance(string PersistenceConnectionString,object achieveObject)
        {
            runtime = new WorkflowRuntime();
            service = new ExternalDataExchangeService();
            perService = new SqlWorkflowPersistenceService(PersistenceConnectionString);
            if (runtime.GetService(service.GetType()) == null)//服务不能重复加入
            {
                runtime.AddService(service);
            }
            if (runtime.GetService(perService.GetType()) == null)
            {
                runtime.AddService(perService);
            }

            if (service.GetService(achieveObject.GetType()) == null)
            {
                service.AddService(achieveObject);//将此类加入外部数据交换服务
            }

            runtime.WorkflowIdled += OnWorkflowIdled;//工作流闲置事件
            runtime.StartRuntime();
        }

        public void OnWorkflowIdled(object sender, WorkflowEventArgs e)
        {
            e.WorkflowInstance.TryUnload();//将内存数据持久化到数据库中
        }


        public void CreateWorkflowAndStart(Type worlflowType)
        {
            instance = runtime.CreateWorkflow(worlflowType);
            instance.Start();
        }


        public void GetWorkflow(Guid workflowId)
        {
            instance=runtime.GetWorkflow(workflowId);
        }


        public Guid GetInstanceId()
        {
            return instance.InstanceId;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值