Learning WF - Part 5

by Raza 29. April 2008 19:03

In this installment we will look at creating custom activities for our workflows. Why do we need a custom activity? This answer is obvious, no matter how many activities are provided by default, there will be a need for the ability to write your own. Why do we need to write a custom activity when we have the code activity where you can write any arbitrary code. Well, it does not give us reusability and the additional benefits that we get with the custom activity.

All actvities are derived from either System.Workflow.ComponentModel.Activity or CompositeActivity. For the current excercise lets derive one from Activity class. First create a new sequential workflow. Then right click the project and add an activity.

By default this activity is derived from the Composite Activity class but for the moment we need a simple activity. So change its property Base Class to System.Workflow.ComponentModel.Activity. Our activity will have the special ability to calculate squares of any number. We need to add two properties to it.

These properties will appear in the Properties windows for this activity. Now the execution of an activity takes place in the Execute function. This function is already defined in the base class, here we need to override it.

The Execute function returns a enum of the type ActivityExecutionStatus to the runtime to indicate what state it is in. The possible states could be:

For the purpose of this tutorial we will just return the closed state to indicate that our work is done. Now lets use the activity we just created. For that do a build on the project and open the workflow. You will see the new activity in toolbox on top.

Drop this activity on your workflow and set its input value property to 2. Then drop a code activity next to it and add the following code:

 

Now run the activity. You can add any code you want here and reuse it.

With that out of the way, lets look at what else we can do with this activity. First we can try to customize its look and feel by decorating the class with some attributes. Lets assign it a nice icon for the designer.

I have used a bitmap file for the icon here. Build the solution so that the icon is recognised and open the designer window now.

You must have noticed the red error sign that appears with the activities if something needs to be specified. We can create such errors of our own by adding a similar attribute. Let's say I want to square values only between 1 and 100. I will need to addd the following class:

 The ActivityValidator class allows you to add the validation functionality. By default the actvities have this class attached to them but you can derive from this class and create your own validation errors. Then this class has to be attached to the particular activity you want to check.

Build and go to the designer, assign different values and see what it looks like.

This concludes the part on creating your own activities. In the next post we will look at host and workflow communication.

Tags: , ,

.Net | WF

Comments

Add comment




  Country flag

biuquote
Loading



Raza Ali

Raza AliI am Currently working as a Senior Developer on .Net and BizTalk Server technologies. I have broad technology interests but find myself more interested in ASP.Net and backend server products. This blog for me is a means to share whatever I come to learn. Hope you find something useful here.  
 E-mail me Send mail

Recent comments

Comment RSS

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008