Windows Workflow Foundation (WF4) - Workflow Services and Auto-Start

This sample application demonstrates how you can create a Workflow Service that will automatically start and periodically wake up to do some work. The sample also demonstrates a UI that shows the progress of the work and provides control and query capabilities.

C# (249.0 KB)
 
 
 
 
 
4 Star
(6)
4,298 times
Add to favorites
4/28/2011
E-mail Twitter del.icio.us Digg Facebook
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BatchAutoStartFactory.cs" company="Microsoft">
//   Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>
//   A Workflow Service Factory that will check the configuration file to determine if there are jobs to submit
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace BatchWeb
{
    using System;
    using System.Configuration;
    using System.ServiceModel;
    using System.ServiceModel.Activities.Activation;
    using System.Threading;
    using System.Threading.Tasks;

    using BatchWeb.Services;

    using BatchWorker.Contracts;
        // Note: Be sure that you reference System.ServiceModel.Activation and not System.WorkflowServices in your project

    /// <summary>
    /// A Workflow Service Factory that will check the configuration file to determine if there are jobs to submit
    /// </summary>
    /// <remarks>
    /// This factory has to be added to a serviceActivation entry in web.config.
    ///   This example shows how you can use the Auto-Start capability of Windows Server AppFabric to take action
    ///   when the service is auto-started.
    ///   Every time a service instance is created, the factory will be invoked to create the WorkflowServiceHost
    ///   giving you the opportunity to configure the service host or some other work that you want to do.
    /// </remarks>
    public class BatchAutoStartFactory : WorkflowServiceHostFactory
    {
        #region Constants and Fields

        /// <summary>
        ///   Flag to determine if this is the first auto start
        /// </summary>
        private static bool autoStarted;

        #endregion

        #region Public Methods

        /// <summary>
        /// Creates a WorkflowServiceHost
        /// </summary>
        /// <param name="constructorString">
        /// The constructor string.
        /// </param>
        /// <param name="baseAddresses">
        /// The base addresses.
        /// </param>
        /// <returns>
        /// A workflow service host
        /// </returns>
        public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
        {
            // Run the auto start check on another thread.
            Task.Factory.StartNew(CheckAutoStartJobs);

            return base.CreateServiceHost(constructorString, baseAddresses);
        }

        #endregion

        #region Methods

        /// <summary>
        /// The check auto start jobs.
        /// </summary>
        private static void CheckAutoStartJobs()
        {
            if (autoStarted)
            {
                return;
            }
            else
            {
                autoStarted = true;
            }

            // Wait a few seconds for the host to spin up
            Thread.Sleep(TimeSpan.FromSeconds(5));

            // Check for autostart
            var autoCount = ConfigurationManager.AppSettings["AutoCount"];

            if (string.IsNullOrWhiteSpace(autoCount))
            {
                return;
            }

            var values = autoCount.Split('|');

            // Check for valid value
            if (values.Length != 2)
            {
                return;
            }

            int count;
            if (!Int32.TryParse(values[0], out count))
            {
                return;
            }

            int delay;
            if (!Int32.TryParse(values[1], out delay))
            {
                return;
            }

            var request = new BatchRequest
                {
                   CountTo = count, Delay = TimeSpan.FromSeconds(delay), StartAt = DateTime.Now, AutoStart = true 
                };

            var proxy = new BatchWorkerClient(
                new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/BatchWeb/BatchWorker.xamlx"));
            try
            {
                var response = proxy.SubmitJob(request);
                proxy.Close();
            }
            catch
            {
                proxy.Abort();
                throw;
            }
        }

        #endregion
    }
}