Retrieving-WF4-Persisted-Workflow-Instances-with-EF43-Code-First

Entity Framework 4.3 has a first class experience for building a database and tables by using code to describe relational metadata and structure of the tables. As changes to the database need to be made, it is a simple process of changing the code to reflect how the database should change. The cool thing is that an existing database can already exist, and it can be indicated to EF's Code-first, that no modifications need to be made to the database or tables, just read the data.

There are many ways to get data out of WF4.x's persistence data store, using the views and tables provided and EF's code-first makes it even easier. This example gets all persisted workflow instances from the System.Activities.DurableInstanceing.Instances view:

Provide the POCO for the Instances view:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Collections.ObjectModel;

namespace WFPersistence.DataModel
{
    public class Instance
    {
        public Guid InstanceId { get; set; }
        public DateTime? PendingTimer { get; set; }
        public DateTime? CreationTime { get; set; }
        public DateTime? LastUpdatedTime { get; set; }
        public int? ServiceDeploymentId { get; set; }
        public string SuspensionExceptionName { get; set; }
        public string SuspensionReason { get; set; }
        public string ActiveBookmarks { get; set; }
        public string CurrentMachine { get; set; }
        public string LastMachine { get; set; }
        public string ExecutionStatus { get; set; }
        public bool? IsInitialized { get; set; }
        public bool? IsSuspended { get; set; }
        public bool? IsCompleted { get; set; }
        public byte? EncodingOption { get; set; }
        public byte[] ReadWritePrimitiveDataProperties { get; set; }
        public byte[] WriteOnlyPrimitiveDataProperties { get; set; }
        public byte[] ReadWriteComplexDataProperties { get; set; }
        public byte[] WriteOnlyComplexDataProperties { get; set; }
        public string IdentityName { get; set; }
        public string IdentityPackage { get; set; }
        public long? Build { get; set; }
        public long? Major { get; set; }
        public long? Minor { get; set; }
        public long? Revision { get; set; }
    }

    public class Instances : Collection<Instance>
    {

    }
}

Next build the DbContext object as follows:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WFPersistence.DataModel
{
    public class WFPersistenceStore : DbContext
    {
        public WFPersistenceStore()
            : base("WFPersist") //Name of the connectionstring setting within the App.config
        {
        }

        public DbSet<Instance> PersistedInstances { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Instance>().ToTable("System.Activities.DurableInstancing.Instances"); //Manages the default schema association with the POCO class
        }
    }
}

Query the View:

private void LoadPersistedInstances()
        {
            var PersistedWFInstances = new Instances();
            try
            {
                using (var PersistStore = new WFPersistenceStore())
                {
                    var result = from t in PersistStore.PersistedInstances
                                 select t;

                    foreach (var instance in result)
                    {
                        PersistedWFInstances.Add(
                        new Instance
                        {
                            InstanceId = instance.InstanceId,
                            PendingTimer = instance.PendingTimer,
                            CreationTime = instance.CreationTime,
                            LastUpdatedTime = instance.LastUpdatedTime,
                            ServiceDeploymentId = instance.ServiceDeploymentId,
                            SuspensionExceptionName = instance.SuspensionExceptionName,
                            SuspensionReason = instance.SuspensionReason,
                            ActiveBookmarks = instance.ActiveBookmarks,
                            CurrentMachine = instance.CurrentMachine,
                            LastMachine = instance.LastMachine,
                            ExecutionStatus = instance.ExecutionStatus,
                            IsInitialized = instance.IsInitialized,
                            IsSuspended = instance.IsSuspended,
                            IsCompleted = instance.IsCompleted,
                            EncodingOption = instance.EncodingOption,
                            ReadWritePrimitiveDataProperties = instance.ReadWritePrimitiveDataProperties,
                            WriteOnlyPrimitiveDataProperties = instance.WriteOnlyComplexDataProperties,
                            ReadWriteComplexDataProperties = instance.ReadWriteComplexDataProperties,
                            WriteOnlyComplexDataProperties = instance.WriteOnlyComplexDataProperties,
                            IdentityName = instance.IdentityName,
                            IdentityPackage = instance.IdentityPackage,
                            Build = instance.Build,
                            Major = instance.Major,
                            Minor = instance.Minor,
                            Revision = instance.Revision
                        });

                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
The results will be loaded within the PersistedWFInstances variable

Currently rated 1.5 by 31 people

  • Currently 1.483871/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: BayerWhite
Posted on: 5/2/2012 at 1:28 PM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed
Comments are closed