NativeActivity Base Class

NativeActivity is an abstract class with a protected constructor. Like CodeActivity, NativeActivity is used for writing imperative behavior by implementing an Execute method. Unlike CodeActivity, NativeActivity has access to all of the exposed features of the workflow runtime through the NativeActivityContext object passed to the Execute method.

Using NativeActivityContext

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

To create a custom activity that inherits from NativeActivity

  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 NativeActivity by adding a base class to the class declaration.

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

    protected override void Execute(NativeActivityContext context)
    {
        Console.WriteLine("Hello World!");
    }
    
  8. Override the CacheMetadata method and call the appropriate Add method to let the workflow runtime know about the custom activity’s variables, arguments, children, and delegates. For more information see the NativeActivityMetadata class.

  9. Use the NativeActivityContext object to schedule a bookmark. See Bookmarks for details on how to create, schedule, and resume a bookmark.

    protected override void Execute(NativeActivityContext context)
        {
            // Create a Bookmark and wait for it to be resumed.
            context.CreateBookmark(BookmarkName.Get(context),
                new BookmarkCallback(OnResumeBookmark));
        }