Hosting WorkflowRuntime 3.5 as a Windows Service

 Download Source

Hosting in a windows service has a few gotcha’s to it as I found out on my first WF project and I wanted to share those here to hopefully help others to get things running much quicker than I did.

Introduction

The code to host in a Windows service is the same as in any other windows application and you can find those details here. However, hosting in a Windows service needs some special handling and this is because you need to call WorkflowRuntime.StartRuntime(), WorkflowRuntime.StopRuntime(), and WorkflowRuntime.Dispose() on the same thread and the SCM (Service Control Manager) which is the Windows service that will call your Windows Services OnStart() and OnStop() methods will call these methods on different threads which is illustrated below. 

SCMThreading

You will want to put WorkflowRuntime.StartRuntime() in your OnStart() method and you will want to put your calls to WorkflowRuntime.StopRuntime() and WorkflowRuntime.Dispose() in the OnStop() method and you will need a way to make sure that all of these calls are preformed on the same thread or you will exprience issues like server hangs on shutdown.

SyncronizationContext

A custom SynchronizationContext is the tool that I used to preform calls on the same thread. My code is based on the threading articles by mikeperetz and I won’t be covering the threading details in this article so please see Mike’s articles for those details. I’ve taken the code in that article series and created my own custom SynchronizationContext called MainThreadSynchronizationContext which is included in the sample code and allows for you to easily marshal calls to the same thread by using the Send method shown below.


    public partial class WorkflowServer : ServiceBase
    {
        private MainThreadSynchronizationContext _syncContext = null;
        WorkflowRuntime _runtime = null;

        public WorkflowServer()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // Create new thread using MainThreadSynchronizationContext and then
            // send message to call Start() on the new "Main WF Thread" thread
            _syncContext = new MainThreadSynchronizationContext("Main WF Thread");
            _syncContext.Send(Start, null);
        }

        // Will be called on "Main WF Thread"
        protected void Start(object data)
        {
            _runtime = new WorkflowRuntime();
            _runtime.StartRuntime();

            // Do other WF related processing
        }

        protected override void OnStop()
        {
            // Call Stop() on "Main WF Thread"
            _syncContext.Send(Stop, null);
        }

        // Will be called on "Main WF Thread"
        protected void Stop(object data)
        {
            _runtime.StopRuntime();
            _runtime.Dispose();
        }
    }

If you use this technique your server will start and stop nicely and you won’t experience any hangs on shutdown which was something we were seeing a lot on my current project.

Tags: , , , ,

10 Responses to “Hosting WorkflowRuntime 3.5 as a Windows Service”

  1. Nicolas says:

    Hello,
    Can i get a one small photo from your blog?
    Thanks
    [url=http://www.noteshamps.com/]Nicolas[/url]

  2. admin says:

    Sure, what were you going to use it for?

  3. ostrov says:

    Thank you,
    very interesting article

  4. Webmaster says:

    Hello! Please e-mail me your contacts. I have a question webmaster@spottovo.ru” rel=”nofollow”>……

    Thank you!!!…

  5. LEON says:


    MedicamentSpot.com. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. High quality drugs. Buy pills online

    Buy:Retin-A.Valtrex.Accutane.Petcam (Metacam) Oral Suspension.Synthroid.Lumigan.Prednisolone.Nexium.Zovirax.Mega Hoodia.Zyban.Arimidex.Human Growth Hormone.100% Pure Okinawan Coral Calcium.Actos.Prevacid….

  6. MAURICE says:


    CheapTabletsOnline.Com. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. High quality pills. Order drugs online

    Buy:VPXL.Viagra.Levitra.Viagra Professional.Viagra Soft Tabs.Cialis Soft Tabs.Cialis Professional.Maxaman.Soma.Propecia.Cialis.Viagra Super Force.Cialis Super Active+.Super Active ED Pack.Viagra Super Active+.Zithromax.Tramadol….

  7. ALFONSO says:


    CheapTabletsOnline.Com. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. Low price drugs. Buy pills online

    Buy:Benicar.Ventolin.Acomplia.Female Pink Viagra.Prozac.Buspar.Lipothin.Nymphomax.Cozaar.Lipitor.Zetia.Amoxicillin.Aricept.Advair.Seroquel.Female Cialis.Wellbutrin SR.Lasix.Zocor.SleepWell….

  8. OTIS says:


    NEW FASHION store. Original designers collection at low prices!!! 20 % TO 70 % OFF. END OF SEASON SALE!!!

    BUY FASHION. TOP BRANDS: GUCCI, DOLCE&GABBANA, BURBERRY, DIESEL, ICEBERG, ROBERTO CAVALLI, EMPORIO ARMANI, VERSACE…

Leave a Reply