Mohammed Atef’s Technical blog

Workflow Foundation 4.0 Extensions

Introduction

Workflow foundation Extensions allow you to add configurable behavior to a workflow solution. The activities that you include in your workflow define the steps that are performed, while the extensions provide the operating environment that these activities are executed in, the persistence extension is not aware of what activities are executed; the extension however, provides the ability to persist those activities (whatever they might be) to a durable store.

SQL Persistence Extension

There are two key aspects of extensions that make then extremely useful. First, as was inferred
earlier, they are configurable. For example, the persistence provider that was used SqlWorkflowInstanceStore was designed to use a SQL Server database. Without changing the
application or the workflow definition.
The second aspect of extensions is that they can be accessed both from the application as well as the
workflow activities. This provides a convenient tool for sharing information between the application and
the workflow.

To use SQL Persistence you need to declare a reference to the InstanceStore class that will be used to persist and load the workflow instances :

   1: private InstanceStore _instanceStore;

Then add you need to Loaded event handler and configures the store to do that you need to write the following code

   1: _instanceStore = new SqlWorkflowInstanceStore(_connectionString);
   2: InstanceView view = _instanceStore.Execute(_instanceStore.CreateInstanceHandle(),
   3: new CreateWorkflowOwnerCommand(),TimeSpan.FromSeconds(30));
   4: _instanceStore.DefaultInstanceOwner = view.InstanceOwner;

where the _connectionstring is the DB sql connection string used for your SQL Persistence.above code has an InstanceStore that is an abstract class from which all persistence providers are derived. An instance of the concrete class SqlWorkflowInstanceStore is created, passing the connection string in the constructor.

The parameters to the Execute() method are a handle (provided by InstanceStore ), a command, and a

timeout value. It returns an InstanceView class, which is roughly analogous to a connection handle.

Now you need to setup The Workflow persistence, to do that add the following codes listed below

   1: i.InstanceStore = _instanceStore;//where i is and instance of WorkflowApplication
   2: i.PersistableIdle = (waiea) => PersistableIdleAction.Unload;
   3: i.Run();

The above code configures the workflow instance to be persisted. First, it sets the InstanceStore

property using the reference created as described early. It then provides an event handler for

the PersistableIdle event, which tells the instance to unload itself from memory. It is persisted to the

database prior to being unloaded.

Custom Extensions

here we will learn how to build a custom extension and how to use it in you Workflow. we are going to build simple custom extension that used to store the connection string, Instead of passing the connection string as an input argument, any activity that needs the connection string can access it from this extension.

Assume that we will create class called  that hold the custom extension and implemented as shown in list below

   1: using System;
   2: public class ConStringExtension
   3:     {
   4:         private string _connectionString = "";
   5:
   6:         public DBExtension(string connectionString)
   7:         {
   8:             _connectionString = connectionString;
   9:         }
  10:
  11:         public string ConnectionString { get { return _connectionString; } }
  12:     }

The above code simply defines a private member that holds the connection string. The value of this string

is passed in the class constructor. A public property is provided for accessing this string.To add the new Custom extension to you Workflow instance add the following code

   1: ConStringExtension _dbExtension = new ConStringExtension(_connectionString);
   2: i.Extensions.Add(_dbExtension);

Finally to use this custom extension into code activity add the following line

   1: ConStringExtension ext = context.GetExtension<ConStringExtension>();

I hope you have understand Workflow extensions well ,now you can check other posts about work flow here

I hope that helped.

Advertisements

September 26, 2010 - Posted by | .Net 2010 | , , , , ,

1 Comment »

  1. […] by Workflow Foundation 4.0 Extensions « Mohammed Atef’s Technical blog | September 26, 2010 | […]

    Pingback by Workflow Foundation 4.0 « Mohammed Atef’s Technical blog | September 26, 2010 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: