Joseph Michael Pesch
VP Programming

Sample Code to Get a Workflow Instance from Persistence DB

by 6. July 2009 03:30

static MyWorkflow GetMyWorkflowInstance(WorkflowRuntime workflowRuntime, Guid instanceID)


{


  /* ================================================================================ */


  // This method is a workaround to get an instance of a workflow from the persistence


  // data store, since there is no direct method to get the workflow instance (i.e.


  // the workflowRuntime.GetWorkflow(instanceId) method gets the workflow model not


  // the actual workflow in it's persisted state.


  /* ================================================================================ */


  // DESERIALIZE FROM PERSISTENCE DB INTO OBJECT.....


  MyWorkflow activity;


  SqlConnection cn = new SqlConnection(


    ConfigurationManager.ConnectionStrings["WorkflowPersistence"].ConnectionString);


  SqlCommand cmd = new SqlCommand(


    "select state from InstanceState where uidInstanceID = @InstanceID", cn);


  cmd.Parameters.AddWithValue("@InstanceID", instanceID.ToString());


  cn.Open();


  byte[] byt = (byte[])cmd.ExecuteScalar();


  cn.Close();


  System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter


    = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();


  formatter.SurrogateSelector


    = System.Workflow.ComponentModel.Serialization.ActivitySurrogateSelector.Default;


  System.IO.MemoryStream stream = new System.IO.MemoryStream(byt);


  stream.Position = 0;


  using (System.IO.Compression.GZipStream stream2


    = new System.IO.Compression.GZipStream(stream,


      System.IO.Compression.CompressionMode.Decompress, true))


  {


    // Here we can finally do the real work to deserialize... 


    activity


      = (MyWorkflow)System.Workflow.ComponentModel.Activity.Load(stream2


      , workflowRuntime.CreateWorkflow(typeof(MyWorkflow)).GetWorkflowDefinition()


      , formatter);


  }


  return activity;


  /* ================================================================================ */


}


 

Tags: ,

ASP.Net | C# | Workflow Foundation

Comments are closed