WF Program Instances


Rather than examine each of the WF runtime services in isolation, we are going to turn next to the representation and management of WF program instances.

In the discussion of the lifecycle of a WF program instance, the roles of the WF runtime services will come into the picture. Our focus here is the lifecycle of a WF program instance because that is the context in which the runtime services are asked to perform their work.

As mentioned at the outset of the chapter, the WorkflowRuntime class contains a set of methods and properties related to the creation and management of WF program instances. These members are shown in Listing 5.2.

Listing 5.2. WorkflowRuntime

 namespace System.Workflow.Runtime {   public class WorkflowRuntime : IServiceProvider, IDisposable   {     /* *** See Listing 5.1 for other members *** */     public WorkflowInstance CreateWorkflow(XmlReader workflowReader);     public WorkflowInstance CreateWorkflow(XmlReader workflowReader,       XmlReader rulesReader,       Dictionary<string, object> namedArgumentValues);     public WorkflowInstance CreateWorkflow(XmlReader workflowReader,       XmlReader rulesReader,       Dictionary<string, object> namedArgumentValues,       Guid instanceId);     public WorkflowInstance CreateWorkflow(Type workflowType);     public WorkflowInstance CreateWorkflow(Type workflowType,       Dictionary<string, object> namedArgumentValues);     public WorkflowInstance CreateWorkflow(Type workflowType,       Dictionary<string, object> namedArgumentValues,         Guid instanceId);     public WorkflowInstance GetWorkflow(Guid instanceId);     public ReadOnlyCollection<WorkflowInstance> GetLoadedWorkflows();     public event EventHandler<WorkflowEventArgs> WorkflowAborted;     public event EventHandler<WorkflowCompletedEventArgs>       WorkflowCompleted;     public event EventHandler<WorkflowEventArgs> WorkflowCreated;     public event EventHandler<WorkflowEventArgs> WorkflowIdled;     public event EventHandler<WorkflowEventArgs> WorkflowLoaded;     public event EventHandler<WorkflowEventArgs> WorkflowPersisted;     public event EventHandler<WorkflowEventArgs> WorkflowResumed;     public event EventHandler<WorkflowEventArgs> WorkflowStarted;     public event EventHandler<WorkflowSuspendedEventArgs>       WorkflowSuspended;     public event EventHandler<WorkflowTerminatedEventArgs>       WorkflowTerminated;     public event EventHandler<WorkflowEventArgs> WorkflowUnloaded;   } } 


The return type of WorkflowRuntime.CreateWorkflow is WorkflowInstance, which represents an in-memory handle to an actual WF program instance. The WorkflowInstance type is defined in the System.Workflow.Runtime namespace and is shown in Listing 5.3.

Listing 5.3. WorkflowInstance

 namespace System.Workflow.Runtime {   public sealed class WorkflowInstance   {     public Guid InstanceId { get; }     public void Abort();     public void Load();     public void Resume();     public void Start();     public void Suspend(string error);     public void Terminate(string error);     public bool TryUnload();     public void Unload();     public Activity GetWorkflowDefinition();     /* *** other members *** */   } } 


WorkflowInstance wraps an underlying WF program instance (an actual set of Activity objects, if the instance is in memory) that is managed by the WF runtime.

When you need a durable handle to a WF program instance, you can use the value of the InstanceId property of WorkflowInstance. This globally unique identifier can always be used to obtain a WorkflowInstance that represents the WF program instance carrying that identifier. As shown here, WorkflowRuntime has a GetWorkflow method that accepts a Guid (which uniquely identifies a WF program instance) and returns a WorkflowInstance:

 static void Main() {   using (WorkflowRuntime runtime = new WorkflowRuntime())   {     runtime.StartRuntime();     WorkflowInstance instance = runtime.CreateWorkflow(...);     Guid handle = instance.InstanceId;     instance = null;     // Get back a handle to the same instance     instance = runtime.GetWorkflow(handle);     ...   } } 





Essential Windows Workflow Foundation
Essential Windows Workflow Foundation
ISBN: 0321399838
EAN: 2147483647
Year: 2006
Pages: 97

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net