⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlworkflowpersistenceservicemanager.cs

📁 基于微软WF开发的工作流全套实例源码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Runtime.Tracking;
using System.Workflow.Runtime.Hosting;
using System.ComponentModel.Design;
using System.Xml;
using System.Reflection;
using System.Workflow.ComponentModel.Compiler;
using System.IO;
using System.CodeDom.Compiler;
using wxwinter.wf.Service;
using System.Workflow.ComponentModel.Serialization;
using System.ComponentModel.Design.Serialization;
using wxwinter.wf.UDPCommunication;
namespace wxwinter.wf.Service
{
	public class SqlWorkflowPersistenceServiceManager : wxwinter.wf.Service.IPersistenceServiceManager
	{

        WorkflowRuntime engine; //管理器所管理的引擎
       

        public SqlWorkflowPersistenceServiceManager(WorkflowRuntime engine)
        {
            this.engine = engine;
        }

        /// <summary>
        /// 得到引擎中的持久化服务对象
        /// </summary>
        /// <returns></returns>
        public SqlWorkflowPersistenceService GetPersistenceService()
        {
            if (engine == null)
            { throw new System.Exception("引擎没有实例化"); }

            SqlWorkflowPersistenceService persistence = engine.GetService<SqlWorkflowPersistenceService>();

            return persistence;
        }

        /// <summary>
        /// 引擎中是否存在持久化服务
        /// </summary>
        /// <returns></returns>
        public bool IsPersistenceServiceInEngine()
        {
            SqlWorkflowPersistenceService persistence = GetPersistenceService();

            if (persistence == null)
            { return false; }
            else
            { return true; }
        }

        /// <summary>
        /// 添加持久化服务到引擎
        /// </summary>
        /// <param name="constring"> SqlWorkflowPersistenceService数据库连拉字串</param>
        /// <returns></returns>
        public string AddPersistenceServiceToEngine(string constring)
        {
            try
            {   //使用SqlConnection测试一下连接情况
                System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constring);
                con.Open();
                con.Close();
            }
            catch
            {
                throw new System.Exception("SqlWorkflowPersistenceService数据库无法连接");
            }

            SqlWorkflowPersistenceService persistence = new SqlWorkflowPersistenceService(constring);
            return AddPersistenceServiceToEngine(persistence);

        }

        /// <summary>
        /// 添加持久化服务到引擎
        /// </summary>
        /// <param name="constring">SqlWorkflowPersistenceService数据库连拉字串</param>
        /// <param name="idelAutoUnload">是否自动将idel持久化</param>
        /// <returns></returns>
        public string AddPersistenceServiceToEngine(string constring, bool idelAutoUnload)
        {
            try
            {   //使用SqlConnection测试一下连接情况
                System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constring);
                con.Open();
                con.Close();
            }
            catch
            {
                throw new System.Exception("SqlWorkflowPersistenceService数据库无法连接");
            }
            TimeSpan locktime = new TimeSpan(0, 10, 0);
            TimeSpan loadtime = new TimeSpan(0, 5, 0);
            SqlWorkflowPersistenceService persistence = new SqlWorkflowPersistenceService(constring, idelAutoUnload, locktime, loadtime);
            return AddPersistenceServiceToEngine(persistence);

        }

        /// <summary>
        /// 添加持久化服务到引擎
        /// </summary>
        /// <param name="persistence">SqlWorkflowPersistenceService对象</param>
        /// <returns></returns>
        public string AddPersistenceServiceToEngine(SqlWorkflowPersistenceService persistence)
        {
            if (engine == null)
            { throw new System.Exception("引擎没有实例化"); }
            if (engine.IsStarted)
            {
                return "引擎正在运行,无法向引擎添加SqlWorkflowPersistenceService服务";
            }

            if (persistence == null)
            {
                throw new System.Exception("SqlWorkflowPersistenceService服务没有实例化");
            }


            if (IsPersistenceServiceInEngine())
            {
                return "引擎中已经存在SqlWorkflowPersistenceService服务";
            }
            else
            {
                engine.AddService(persistence);
                return "SqlWorkflowPersistenceService服务已添加到引擎中";
            }

        }

        /// <summary>
        /// 从引擎中移出持久化服务
        /// </summary>
        /// <returns></returns>
        public string RemovePersistenceServiceFromEngine()
        {
            if (engine == null)
            { throw new System.Exception("引擎没有实例化"); }

            if (engine.IsStarted)
            {
                return "引擎正在运行,无法移除SqlWorkflowPersistenceService服务";
            }

            SqlWorkflowPersistenceService persistence = GetPersistenceService();

            if (persistence == null)
            {
                return "引擎中不存在SqlWorkflowPersistenceService服务";

            }
            else
            {
                engine.RemoveService(persistence);
                return "SqlWorkflowPersistenceService服务已移出";

            }
        }


        public System.Data.DataSet GetAllWorkflows()
        {
            System.Data.DataSet ds = new DataSet();
            ds.Tables.Add(new  DataTable());
            ds.Tables[0].Columns.Add("WorkflowInstanceId");
            ds.Tables[0].Columns.Add("IsBlocked");
            ds.Tables[0].Columns.Add("Status");
            ds.Tables[0].Columns.Add("NextTimerExpiration");

            SqlWorkflowPersistenceService SWPS =this.GetPersistenceService();
            System.Collections.IEnumerable workflows = SWPS.GetAllWorkflows();
            foreach (SqlPersistenceWorkflowInstanceDescription instanceDescription in workflows)
            {
                string v = instanceDescription.SuspendOrTerminateDescription;
                Guid guid = instanceDescription.WorkflowInstanceId;
                bool b = instanceDescription.IsBlocked;
                DateTime time = instanceDescription.NextTimerExpiration.Value;
                WorkflowStatus st = instanceDescription.Status;
                //WorkflowStatus.Completed 
                //WorkflowStatus.Created 
                //WorkflowStatus.Running 
                //WorkflowStatus.Suspended 
                //WorkflowStatus.Terminated 

                ds.Tables[0].Rows.Add(new object[] { guid.ToString(), b.ToString(), time.ToString(), st.ToString() });

            }
            return ds;

        }


        //public void x()
        //{
        //    SqlWorkflowPersistenceService persistence = GetPersistenceService(); 
        //    foreach (Guid guid in persistence.LoadExpiredTimerWorkflowIds())
        //    {
        //        string v = guid.ToString();
        //        System.Console.WriteLine(v);
        //    }

        //}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -