Compartilhar via


Task 1: Create the Custom Activity

Download sample

In this task, you create the WebTearActivity class. This class derives from the Activity class.

When the workflow that contains your custom activity starts, the Windows Workflow run-time engine calls your overridden Execute method and passes an ActivityExecutionContext object as a parameter.

When you create the WebTear activity, you create a DependencyProperty object in the class that makes custom data available that your activity must have to complete processing. The DependencyProperty object that is used in this activity is a String object. It is used for the Web page URL.

Note

Although you are encouraged to follow the exercises in a linear manner, it is not required. You can start this exercise by opening the sample project and proceeding to the steps in the following section.

To create the WebTear custom activity

  1. In Solution Explorer, right click the WebTearWorkflow project, click Add and Activity. Change the activity name to WebTearActivity.

To declare the WebTearActivity class

  1. Open the WebTearActivity in the code editor by right-clicking it and selecting View Code. Change its parent class Activity as shown in the following example.

        Public Class WebTearActivity : Inherits Activity
        End Class
    
        public class WebTearActivity : Activity
        {
        }
    

To define the PageFinished event

  1. In the WebTearActivity class, create a new delegate named PageFinishedEventHandler that accepts a Object parameter named sender and a PageFinishedEventArgs parameter named e.

    Note

    You create the PageFinishedEventArgs in step 3.

    public delegate void PageFinishedEventHandler(object sender,
        PageFinishedEventArgs e);
    
  2. In the WebTearActivity class, create a new private PageFinishedEventHandler event named pageFinishedEvent.

  3. In the WebTearActivity class, create a public PageFinishedEventHandler event property named PageFinished.

  4. In the PageFinished event property, create an add and remove accessor to add and remove event handlers from the pageFinishedEvent event.

    private event PageFinishedEventHandler pageFinishedEvent;
    public event PageFinishedEventHandler PageFinished
    {
        add
        {
            pageFinishedEvent += value;
        }
        remove
        {
            pageFinishedEvent -= value;
        }
    }
    
  5. Right-click on the WebTearWorkflow project, and select Add and Class. Name the new class PageFinishedEventArgs, and make the class public.

    Public Class PageFinishedEventArgs
    End Class
    
    public class PageFinishedEventArgs
    {
    }
    
  6. In the PageFinishedEventArgs class, declare a private String field named pageData.

    private string pageData;
    
  7. In the PageFinishedEventArgs class, create a public String property named Data.

    In this property, create a get method that returns the value of the pageData field that you created in the previous step.

    public string Data
    {
        get { return this.pageData; }
    }
    
  8. In the PageFinishedEventArgs class, create a public constructor that accepts a String parameter named data.

    In the body of the constructor, set the pageData field that is defined in the PageFinishedEventArgs class equal to the value of the data parameter.

    public PageFinishedEventArgs(string data)
    {
        this.pageData = data;
    }
    

To define the URL DependencyProperty

  1. In the WebTearActivity class, create a new public static DependencyProperty field named UrlProperty.

    Set this field equal to the return value of the Register static method, passing the string "Url", the type of the String class, and the type of the WebTearActivity class as parameters to the Register method.

    public static DependencyProperty UrlProperty =
                DependencyProperty.Register("Url",
                typeof(System.String),
                typeof(WebTearActivity));
    
  2. In the WebTearActivity class, create a public string property named Url.

  3. Apply the DesignerSerializationVisibilityAttribute to the Url property in the WebTearActivity class, passing the Visible value as a parameter to the DesignerSerializationVisibilityAttribute.

  4. Apply the BrowsableAttribute to the Url property in the WebTearActvity class, passing true as a parameter to the BrowsableAttribute.

  5. Apply the DescriptionAttribute to the Url property in the WebTearActvity class, passing the string "Url to download" as a parameter to the DescriptionAttribute.

  6. Apply the CategoryAttribute to the Url property in the WebTearActvity class, passing the string "WebTearActivity Property" as a parameter to the CategoryAttribute.

  7. Define a get method for the Url property.

  8. In the get method body, return the value that is received from calling the GetValue base class method, passing UrlProperty as the value to retrieve.

  9. Define a set method for the Url property.

    In the set method body, call the SetValue base class method, passing UrlProperty as the value to set.

    [DesignerSerializationVisibilityAttribute
        (DesignerSerializationVisibility.Visible)]
    [BrowsableAttribute(true)]
    [DescriptionAttribute("Url to download")]
    [CategoryAttribute("WebTearActivity Property")]
    public string Url
    {
        get
        {
            return ((string)(base.GetValue(WebTearActivity.UrlProperty)));
        }
        set
        {
            base.SetValue(WebTearActivity.UrlProperty, value);
        }
    }
    

To define the Execute method

  1. In the WebTearActivity class, override the Execute base class method.

  2. In the Execute method, create a local String variable named pageData.

  3. Create a local WebClient variable named client and create an instance of that object.

  4. Create a try block.

    In the body of that block, call the DownloadString method of the client object, passing the Url property of your class as a parameter.

  5. Create a catch block for the Exception exception after the try block that you created in the previous step.

    In the body of the catch block, set the pageData field equal to the Message property of the exception.

  6. Raise the PageFinished event that is defined in the WebTearActivity class, passing the following as parameters:

    • null (Nothing in Visual Basic).

    • A new PageFinishedEventArgs object that uses pageData as the constructor parameter.

  7. Return Closed from the Execute method to notify the Windows Workflow run-time engine that your activity has finished. Your final Execute method should appear similar to the following code.

    protected override ActivityExecutionStatus Execute
        (ActivityExecutionContext context)
    {
        string pageData;
        System.Net.WebClient client = new System.Net.WebClient();
    
        try
        {
            // Download the web page data
            pageData = client.DownloadString(this.Url);
        }
        catch (Exception e)
        {
            pageData = e.Message;
        }
    
        // Raise the PageFinished event back to the host
        pageFinishedEvent(null, new PageFinishedEventArgs(pageData));
    
        // Notifiy the runtime that the activity has finished
        return ActivityExecutionStatus.Closed;
    }
    

Compiling the Code

For more information about compiling your code, see Compiling the Code.

In Task 2: Use the Custom Activity in a Sequential Workflow, you add the custom activity that you just created to a sequential workflow.

See Also

Reference

Activity
Execute
ActivityExecutionContext
DependencyProperty
ActivityToolboxItem
Register
DependencyProperty

Other Resources

Task 2: Use the Custom Activity in a Sequential Workflow
Tutorial: Create a Custom WF Activity

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04