/// <summary>
        /// 获取工作流配置
        /// </summary>
        /// <returns></returns>
        public WorkflowConfig GetConfig()
        {
            string         cacheName = "workflowconfig";
            WorkflowConfig config    = null;

            _memoryCache.TryGetValue <WorkflowConfig>(cacheName, out config);
            if (config == null)
            {
                string fileName = GetFileName("workflowconfig.xml");
                config = ReadConfig(fileName);
                _memoryCache.Set(cacheName, config);
            }
            return(config);
            //if (HttpContext.Current == null)
            //{
            //	string fileName = GetFileName("workflowconfig.xml");
            //	WorkflowConfig config = ReadConfig(fileName);
            //	return config;
            //}
            //if (HttpContext.Current.Cache[cacheName] == null)
            //{
            //	string fileName = GetFileName("workflowconfig.xml");
            //	WorkflowConfig config = ReadConfig(fileName);
            //	CacheDependency fileDependency = new System.Web.Caching.CacheDependency(fileName);
            //	HttpContext.Current.Cache.Add(cacheName, config, fileDependency, DateTime.MaxValue, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            //}
            //return (WorkflowConfig)HttpContext.Current.Cache[cacheName];
        }
        private WorkflowConfig ReadConfig(string fileName)
        {
            WorkflowConfig config        = null;
            XmlSerializer  xmlSerializer = new XmlSerializer(typeof(WorkflowConfig));

            using (XmlReader reader = XmlReader.Create(fileName))
            {
                config = (WorkflowConfig)xmlSerializer.Deserialize(reader);
            }
            return(config);
        }
        /// <summary>
        /// Gets the root.
        /// </summary>
        /// <param name="applicationName">Name of the application.</param>
        /// <returns></returns>
        public string GetRoot(string applicationName)
        {
            WorkflowConfig config = GetConfig();

            if (config == null)
            {
                throw new ArgumentException("workflow config xml file is miss or wrong format");
            }
            List <string>       names       = new List <string>();
            WorkflowApplication application = config.Get(applicationName);

            return(application.Root);
        }
        /// <summary>
        /// Gets all workflow.
        /// </summary>
        /// <returns></returns>
        public string[] GetAllWorkflowDefineName()
        {
            WorkflowConfig config = GetConfig();

            if (config == null)
            {
                throw new ArgumentException("workflow config xml file is miss or wrong format");
            }
            List <string> workflows             = new List <string>();
            List <WorkflowDefineConfig> configs = new List <WorkflowDefineConfig>();

            foreach (WorkflowApplication app in config.Applications)
            {
                configs.AddRange(app.Defines);
            }
            foreach (WorkflowDefineConfig one in configs)
            {
                if (one.Enabled)
                {
                    workflows.Add(one.Name);
                }
            }
            return(workflows.ToArray());
        }
        /// <summary>
        /// Gets all workflow.
        /// </summary>
        /// <returns></returns>
        public WorkFlowDefine[] GetAllWorkflow(string applicationName)
        {
            WorkflowConfig config = GetConfig();

            if (config == null)
            {
                throw new ArgumentException("workflow config xml file is miss or wrong format");
            }
            List <WorkFlowDefine> workflows   = new List <WorkFlowDefine>();
            WorkflowApplication   application = config.Get(applicationName);

            if (application == null)
            {
                throw new NullReferenceException(string.Format("{0} cannot found", applicationName));
            }
            foreach (WorkflowDefineConfig one in application.Defines)
            {
                if (one.Enabled)
                {
                    workflows.Add(GetWorkflowDefine(one.Name));
                }
            }
            return(workflows.ToArray());
        }
        /// <summary>
        /// 按照工作流名称获取工作流定义
        /// </summary>
        /// <param name="workflowName">工作流名称</param>
        /// <returns></returns>
        public WorkFlowDefine GetWorkflowDefine(string workflowName)
        {
            string         cacheName = GetCacheName(workflowName);
            WorkFlowDefine wf        = null;

            _memoryCache.TryGetValue <WorkFlowDefine>(cacheName, out wf);
            if (wf == null)
            {
                WorkflowConfig config = GetConfig();
                bool           found  = false;
                foreach (WorkflowApplication app in config.Applications)
                {
                    for (int i = 0; i < app.Defines.Count; i++)
                    {
                        if (app.Defines[i].Name.Equals(workflowName, StringComparison.OrdinalIgnoreCase))
                        {
                            string fullFileName = GetFileName(app.Defines[i].File);
                            //CacheDependency fileDependency = new System.Web.Caching.CacheDependency(fullFileName);
                            // HttpContext.Current.Cache.Add(cacheName, ReadFromFile(app, fullFileName), fileDependency, DateTime.MaxValue, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                            wf = ReadFromFile(app, fullFileName);
                            _memoryCache.Set <WorkFlowDefine>(cacheName, wf);
                            found = true;
                            break;
                        }
                    }
                }
                if (!found)
                {
                    throw new ArgumentException(string.Format("workflowName '{0}' must in config", workflowName));
                }
            }
            return(wf);
            //         if (HttpContext.Current == null) //非http应用,无法从缓存中获取,只能读取.
            //{
            //	//List<WorkflowDefineConfig> defines = new List<WorkflowDefineConfig>();
            //	WorkflowConfig config = GetConfig();
            //	foreach (WorkflowApplication app in config.Applications)
            //	{
            //		for (int i = 0; i < app.Defines.Count; i++)
            //		{
            //			if (app.Defines[i].Name.Equals(workflowName, StringComparison.OrdinalIgnoreCase))
            //			{
            //				string fullFileName = GetFileName(app.Defines[i].File);
            //				return ReadFromFile(app, fullFileName);
            //			}
            //		}
            //	}
            //	throw new ArgumentException(string.Format("workflowName '{0}' must in config", workflowName));
            //}


            ////有缓冲对象,则从缓存对象中获取
            ////获取缓存名字
            //string cacheName = GetCacheName(workflowName);
            ////从缓存中读取
            //if (HttpContext.Current.Cache[cacheName] == null)
            //{
            //	WorkflowConfig config = GetConfig();
            //	bool found = false;
            //	foreach (WorkflowApplication app in config.Applications)
            //	{
            //		for (int i = 0; i < app.Defines.Count; i++)
            //		{
            //			if (app.Defines[i].Name.Equals(workflowName, StringComparison.OrdinalIgnoreCase))
            //			{
            //				string fullFileName = GetFileName(app.Defines[i].File);
            //				CacheDependency fileDependency = new System.Web.Caching.CacheDependency(fullFileName);
            //				HttpContext.Current.Cache.Add(cacheName, ReadFromFile(app,fullFileName), fileDependency, DateTime.MaxValue, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            //				found = true;
            //				break;
            //			}
            //		}
            //	}
            //	if(!found)
            //		throw new ArgumentException(string.Format("workflowName '{0}' must in config", workflowName));
            //}
            //return (WorkFlowDefine)HttpContext.Current.Cache[cacheName];
        }