Creating Custom Workflow Activity In Windows Workflow Foundation

Workflow Foundation

Microsoft has provided us a very strong foundation since the release of .NET Framework 3.5 known as Windows Workflow Foundation (WF), and it is getting much better with every release. The latest one is WF 4.0 and is really useful in mapping your business processes to workflows. The basic idea behind any workflow creation is to automate the business processes and do it more declarative way.

Need of Custom Activities

Though the framework provides you many builtin activities so that you can grab them and directly use them in your workflow application as it is, but again in most of the cases business scenarios are complex and requirements cannot be solved directly using out of the box activities. So in such cases what needs to be done? No need to worry, similar to SharePoint workflows, windows workflow foundation also supports creating custom activities. (Awkward to say but WF seems much stronger when compared to SharePoint workflow engine). You can also provide input parameters to your activity and activity can return you output that can be later used in the workflow.

Example

Okay enough of theory and let's get in to the action.

I have intentionally taken a very simple example for the demo, it simply uses a custom activity and workflow and simply adds a new product in the products database and finishes. The purpose is to understand the creation and execution of the custom activity.

I will start by creating a project in Visual Studio 2012.

Open Visual Studio and click on Create new project. From the left side of the new project window, click on the workflow category and select Workflow Console Application project.

You will see a Workflow.xaml opened in the editor. This is basically a playground for you where you can start designing you workflow.

If you have a look at the left side of the window, you will see something familiar as Toolbox (remember ASP.NET web applications?) and here is the place where you will find all the out-of-the box activities provided by WF 4.0.

I have designed a very simple workflow that prints a line to the windows when the workflow starts and does some execution of activities and then again prints a line to a window when the workflow finishes.

workflow

Now wait a minute, did you observe something called "AddProduct" there in the editor as well as in the toolbox? From where on the earth that come?

Nothing suspicious going on here so don't worry, this is the custom activity that I created that simply adds a new product in the products inventory. I won't go into much details of the how I created the database and used it with Entity Framework but we will concentrate more on creating that custom activity and passing parameters to it.

Implementation

Custom activities in the windows workflow foundation are nothing but the class that is derived from a class known as "CodeActivity".

Once you create your own class and derive it from CodeActivity , you will need to override the execution method of the parent class. This method gives you the workflow execution context so that you can play around with input and output parameters.

The following code is just for educational purposes of how to create a custom activity, you can add any complex logic in the execute method.

So here is the AddProduct activity code:

  1. public class AddProduct : CodeActivity  
  2. {  
  3.   public InArgument<int> ProductId { getset; }  
  4.   public InArgument<string> ProductName { getset; }  
  5.   public InArgument<string> ProductDescription { getset; }  
  6.   public InArgument<int> ProductPrice { getset; }  
  7.   public InArgument<int> ProductDiscount { getset; }  
  8.   public OutArgument<string> Result { getset; }  
  9.   
  10.   protected override void Execute(CodeActivityContext context)  
  11.   {  
  12.     try  
  13.     {  
  14.       using (var productEntities = new ProductsEntities())  
  15.       {  
  16.         productEntities.ProductInfoes.Add(new ProductInfo  
  17.         {  
  18.            ProductId = ProductId.Get(context),  
  19.            ProductName = ProductName.Get(context),  
  20.            ProductDescription = ProductDescription.Get(context),  
  21.            ProductPrice = ProductPrice.Get(context),  
  22.            ProductDiscount = ProductDiscount.Get(context)  
  23.          });  
  24.          productEntities.SaveChanges();  
  25.         }  
  26.                 
  27.     context.SetValue(Result, "Product with Id : " + context.GetValue(ProductId) + " Added Successfully!!");  
  28.      }  
  29.      catch (Exception ex)  
  30.      {  
  31.        context.SetValue(Result, "Error While Adding Product : " + ex.Message);  
  32.      }  
  33.   
  34.    }  
  35.   }  
As you can see, this activity has 5 input parameters and one output parameter that is later consumed by the workflow write line activity (at the end of the workflow) to display the message to users.

Once you have created the class and done the implementation of the execute method, then just rebuild your solution and observe the toolbox. The framework automatically adds the custom activity for you in the toolbox.

Now the question is, how to pass parameters to the activity?

Once the activity is available in the toolbox, simply drag and drop to the workflow. Right-click on the activity and go to the properties window.

Here you will need to provide the input parameters and also if you are returning something from your custom activity then you will need to catch that output to some workflow variable.

workflow variable

I am passing some hardcoded values as parameters to the activity but this can be made dynamic by passing in workflow arguments.

The result is nothing but the output message that our activity is producing from execute method. I have simply assigned that result to the workflow variable called varResult, so that I will be able to use it in the workflow context.

That's all guys, I hope now you understand how to create custom activities in Windows Workflow Foundation and use them to make your life easier.


Similar Articles