http://www.developer.com/net/net/article.php/3658371/Building-a-Custom-Activity-in-Windows-Workflow-Foundation.htm
Activities are the building blocks of Windows Workflow Foundation (WF) workflows. They represent the basic steps within a workflow, which is constructed as a tree of activities. Activities make up the individual units of execution, and they allow for re-use. You can compose a solution by assembling activities, which themselves can be compositions of more than one activity. There are two types of WF activity: basic and composite. A basic activity is custom coded to provide its function set. A composite activity is built out of other existing activities. A WF base activity library provides out-of-the-box activities, but you also can write custom activities or obtain them from third-party sources. The following are some common activities included in the activity library: As you can see, some powerful capabilities are available by default. You can use any of these to arrange a workflow. Along with being rules driven, activities can support transactions and sequential control. The activities' declarative rules support is very powerful because it allows you to alter or change the logic without recompiling code. WF is a very powerful framework for building workflow-enabled applications, but its existing out-of-the-box activities likely won't enable you to accomplish everything you want. That's where custom activities come in. Rather than just embedding code, you can model custom application logic as activities in WF solutions and use it to encapsulate reusable business logic. This ensures that you stay within the WF execution model, as well as helping to ensure visibility and control of workflow solutions. It also is essential for the ability to provide dynamic updates. A specific activity component model controls building custom activities. Each activity has a set of associated components. You associate the components with the activity by using attributes on the activity definition. If a particular component is not specified, then the parent's attributes are applied instead. The components are: One of the properties that you can control in your custom activity is the base class. A number of types are available, including the following: System.Workflow.Activities.SequenceActivity is the default activity type. The following components are required to build the examples in this article: I downloaded them and set up a .NET Framework 3.0 Virtual PC environment. I've found this to be a good practice for trying out new things without the risk of messing up my current environment and while preserving various environments. It is especially handy when you intermittently need to support a particular environment that is outside your normal development. The example is a "Hello World!" sequential activity similar to the one built in "Get Ready for Windows Workflow Foundation." In this case, you'll turn the prior sequential workflow into a composite activity that you can use in its entirety. To begin, use the following steps to create a sequential workflow within Visual Studio 2005: Figure 1. Diagram of the Completed Composite Activity Here is a copy of the code that my project created. You should see a Program.cs that contains the main execution point for the console application and contains auto-generated code: Here is the code from the HelloWorldActivity.xoml.cs code-behind file: Although this is a fairly simple example, it gives you an idea of how to build a sequential workflow. You could expand the example to use the IfElse activity to display the "Hello World!" text based on various languages, or you could just further explore other activities. Here are a few additional items about custom activities that you may find beneficial: Custom activities that contain custom logic for workflows ensure that the logic operates within the workflow model and allow transparency and dynamic update. Hopefully, this tutorial got you started on building your own custom activities and prepared you to continue building more. The topic of the next column is yet to be determined. It will likely cover either state machine workflow or building rules-based workflows using WF. However, if you have something in particular that you would like to see explained here, you could reach me at mstrawmyer@crowechizek.com. Mark Strawmyer (MCSD, MCSE, MCDBA) is a senior architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the fourth year in a row. You can reach Mark at mstrawmyer@crowechizek.com.
Building a Custom Activity in Windows Workflow Foundation
February 7, 2007
Custom Activities
Building a Custom Activity

Click here for a larger image.using System;using System.Collections.Generic;using System.Text;using System.Threading;using System.Workflow.Runtime;using System.Workflow.Runtime.Hosting;namespace HelloWorldWorkflow{ class Program { static void Main(string[] args) { using(WorkflowRuntime workflowRuntime = new WorkflowRuntime()) { AutoResetEvent waitHandle = new AutoResetEvent(false); workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();}; workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) { Console.WriteLine(e.Exception.Message); waitHandle.Set(); }; WorkflowInstance instance = workflowRuntime.CreateWorkflow( typeof(HelloWorldWorkflow.HelloWorkflow)); instance.Start(); waitHandle.WaitOne(); } } }}using System;using System.ComponentModel;using System.ComponentModel.Design;using System.Collections;using System.Drawing;using System.Workflow.ComponentModel.Compiler;using System.Workflow.ComponentModel.Serialization;using System.Workflow.ComponentModel;using System.Workflow.ComponentModel.Design;using System.Workflow.Runtime;using System.Workflow.Activities;using System.Workflow.Activities.Rules;namespace HelloWorldActivityLibrary{ public partial class HelloWorldActivity : SequenceActivity { private bool isTrue = false; public bool IsTrue { get { return this.isTrue; } set { this.isTrue = value; } } private void helloActivity_ExecuteCode(object sender, EventArgs e) { Console.Write("Hello World!"); Console.ReadLine(); } }}Other Considerations
Custom Logic For Workflows
Future Columns
About the Author