Click here to Skip to main content
15,886,873 members
Articles / Programming Languages / C#

Windows Workflow Foundation ASP.NET State Machine

Rate me:
Please Sign up or sign in to vote.
4.00/5 (17 votes)
12 May 2006CPOL5 min read 154.3K   1.5K   46  
Single Page State Machine workflow
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Web.Caching;
using System.Collections.Specialized;
using System.IO;
using System.Workflow.Activities;
using System.Web.Configuration;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace JQD
{
    /// <summary>
    ///  This is modified from Jon Flanders' Blog ASPNET Workflow example.
    ///  Local and ManualScheduler services are attached to Workflow runtime.
    /// </summary>
    public class WorkflowHost : IHttpModule
    {
        static object _sync = new object();
        static string _WFName = "WorkflowRuntimeWithServices";

        #region Build a runtime with Services and cache it

        public static WorkflowRuntime RuntimeWithServices
        {
            get
            {
                object owr = HttpContext.Current.Cache[_WFName]; // Cache is per AppDomain
                WorkflowRuntime wr = null;
                if (owr == null)
                {
                    lock (_sync)
                    {
                        // need to check again since another thread could have just created 
                        // one WF runtime while this thread is waiting for the lock
                        owr = HttpContext.Current.Cache[_WFName]; 
                        if (owr == null)   
                        {
                            wr = new WorkflowRuntime();
                            wr.AddService(new ManualWorkflowSchedulerService());
                            ExternalDataExchangeService de = new ExternalDataExchangeService();
                            wr.AddService(de);
                            de.AddService(new JQD.LocalService());  // each solution need to change this !!!
                            wr.StartRuntime();
                            HttpContext.Current.Cache.Add(_WFName, wr, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
                        }
                        else
                            wr = owr as WorkflowRuntime;
                    }
                }
                else
                    wr = owr as WorkflowRuntime;
                return wr;
            }
        }

        #endregion

        #region other code for Http Module

        public void Dispose() { }
        public void Init(HttpApplication app)
        {
            app.BeginRequest += new EventHandler(app_BeginRequest);
        }
        void app_BeginRequest(object sender, EventArgs e) { }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I am a Microsoft Certified Application Developer (MCAD), currently focusing on using .Net Framework to develop Business Solutions. I am mostly language neutral. I have used C, C++, ATL, MFC, VB.Net, C#, VB 6, PL/SQL, Transact SQL, ASP, Fortran, etc.

Comments and Discussions