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="StopJob.aspx.cs" company="Microsoft">
//   Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>
//   The stop job.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace BatchWeb
{
    using System;
    using System.ServiceModel;
    using System.Web.UI;

    using BatchWeb.Services;

    /// <summary>
    /// The stop job.
    /// </summary>
    public partial class StopJob : Page
    {
        #region Methods

        /// <summary>
        /// The page_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void Page_Load(object sender, EventArgs e)
        {
            var stopJob = this.Request.QueryString["JobId"];
            if (!string.IsNullOrEmpty(stopJob))
            {
                this.Stop(stopJob);
            }
            else
            {
                this.LabelJobID.Text = "Invalid Job ID";
            }
        }

        /// <summary>
        /// The stop.
        /// </summary>
        /// <param name="stopJob">
        /// The stop job.
        /// </param>
        private void Stop(string stopJob)
        {
            Guid jobId;
            if (Guid.TryParse(stopJob, out jobId))
            {
                var proxy = new BatchWorkerClient();
                try
                {
                    var batchData = this.Session["BatchJobs"] as BatchJobData;
                    var job = proxy.StopJob(jobId);
                    if (batchData != null)
                    {
                        batchData.Remove(jobId);
                    }

                    // Update the job
                    proxy.Close();
                    this.LabelJobID.Text = job.JobId.ToString();
                    this.LabelCurrentCount.Text = job.CurrentCount.ToString();
                    this.LabelCountTo.Text = job.CountingTo.ToString();
                }
                catch (FaultException fex)
                {
                    proxy.Abort();
                    if (fex.Message.Contains("InstancePersistenceCommand"))
                    {
                        this.LabelMsg.Text = "Error stopping job - it may already have completed";
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }

        #endregion
    }
}