July 9, 2020
Hot Topics:

Building a Custom Activity in Windows Workflow Foundation

  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Here is the code from the HelloWorldActivity.xoml.cs code-behind file:

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();      }   }}

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.

Other Considerations

Here are a few additional items about custom activities that you may find beneficial:

  • Get in the habit of including only basic accessor get and set functionality in your properties, and do not include code in the constructor either. I've been told this will help you avoid issues when using SharePoint and WF together.
  • When building custom activities, ensure that your custom activity goes through a dehydration cycle during testing to ensure it behaves properly.
  • Building a basic activity (pure custom code) isn't really that much different from building a composite. You have more items, such as the component model, to consider, but for the most part the steps are the same.
  • Depending on the purpose of your activity, you may want to further explore the ToolboxItem, Designer, Theme, and Validator classes to enrich the developer experience when using your custom activity.

Custom Logic For Workflows

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.

Future Columns

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.

About the Author

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.

Page 2 of 2

This article was originally published on February 7, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date