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,302 times
Add to favorites
4/28/2011
E-mail Twitter del.icio.us Digg Facebook
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BatchJobData.cs" company="Microsoft">
//   Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>
//   The batch job data.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace BatchWeb
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ServiceModel;

    using BatchWeb.Services;

    using BatchWorker.Contracts;

    /// <summary>
    /// The batch job data.
    /// </summary>
    public class BatchJobData : IListSource
    {
        #region Constants and Fields

        /// <summary>
        /// The _batch jobs.
        /// </summary>
        private readonly List<BatchInfoResponse> batchJobs = new List<BatchInfoResponse>();

        #endregion

        #region Properties

        /// <summary>
        /// Gets BatchJobs.
        /// </summary>
        public List<BatchInfoResponse> BatchJobs
        {
            get
            {
                return this.batchJobs;
            }
        }

        /// <summary>
        /// Gets a value indicating whether ContainsListCollection.
        /// </summary>
        public bool ContainsListCollection
        {
            get
            {
                return false;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The get all jobs.
        /// </summary>
        /// <returns>
        /// All jobs in the collection
        /// </returns>
        public IEnumerable<BatchInfoResponse> GetAllJobs()
        {
            return this.batchJobs;
        }

        /// <summary>
        /// The query jobs.
        /// </summary>
        public void QueryJobs()
        {
            var proxy = new BatchWorkerClient();
            for (var i = 0; i < this.batchJobs.Count; i++)
            {
                try
                {
                    if (!this.batchJobs[i].Completed)
                    {
                        var response = proxy.QueryJob(this.batchJobs[i].JobId);
                        this.batchJobs[i] = response;
                    }
                }
                catch (FaultException fex)
                {
                    proxy.Abort();

                    // Renew proxy after fault
                    proxy = new BatchWorkerClient();

                    if (fex.Message.Contains("InstancePersistenceCommand"))
                    {
                        this.batchJobs[i].Completed = true;
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (proxy.InnerChannel.State == CommunicationState.Opened)
            {
                proxy.Close();
            }
        }

        #endregion

        #region Implemented Interfaces

        #region IListSource

        /// <summary>
        /// The get list.
        /// </summary>
        /// <returns>
        /// the list of jobs
        /// </returns>
        public IList GetList()
        {
            return this.batchJobs;
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// The remove.
        /// </summary>
        /// <param name="jobId">
        /// The job id.
        /// </param>
        internal void Remove(Guid jobId)
        {
            var jobIndex = this.batchJobs.FindIndex((b) => b.JobId == jobId);
            if (jobIndex >= 0)
            {
                this.batchJobs.RemoveAt(jobIndex);
            }
        }

        #endregion
    }
}