This documentation is archived and is not being maintained.

Workflow Activity Authoring Using the CodeActivity Class

This topic applies to Windows Workflow Foundation 4 (WF4).

Activities created by inheriting from CodeActivity can implement basic imperative behavior by overriding the Execute method.

Using CodeActivityContext

Features of the workflow runtime can be accessed from within the Execute method by using members of the context parameter, of type CodeActivityContext. The features available through CodeActivityContext include the following:

  • Getting and setting the values of variables and arguments.

  • Custom tracking features using Track.

  • Access to the activity’s execution properties using GetProperty.

To create a custom activity that inherits from CodeActivity

  1. OpenVisual Studio 2010.

  2. Select File, New, and then Project. Select Workflow 4.0 under Visual C# in the Project Types window, and select the v2010 node. Select Activity Library in the Templates window. Name the new project HelloActivity.

  3. Right-click Activity1.xaml in the HelloActivity project and select Delete.

  4. Right-click the HelloActivity project and select Add , and then Class. Name the new class HelloActivity.cs.

  5. In the HelloActivity.cs file, add the following using directives.

    using System.Activities;
    using System.Activities.Statements;
  6. Make the new class inherit from CodeActivity by adding a base class to the class declaration.

    class HelloActivity : CodeActivity
  7. Add functionality to the class by adding an Execute method.

    protected override void Execute(CodeActivityContext context)
        Console.WriteLine("Hello World!");
  8. Use the CodeActivityContext to create a tracking record.

    protected override void Execute(CodeActivityContext context)
        Console.WriteLine("Hello World!");
        CustomTrackingRecord record = new CustomTrackingRecord("MyRecord");
        record.Data.Add(new KeyValuePair<String, Object>("ExecutionTime", DateTime.Now));