15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

OC286 ❘ ChaPTer 57 windOws wOrkflOw fOundAtiOn 3.0<br />

activities using this example. When creating custom activities, you can simply construct a class within a<br />

workflow project; however, it is preferable to construct your custom activities inside a separate assembly,<br />

because the Visual Studio design time environment (<strong>and</strong> specifically workflow projects) will load activities<br />

from your assemblies <strong>and</strong> can lock the assembly that you are trying to update. For this reason, you should<br />

create a simple class library project to construct your custom activities within.<br />

A simple activity such as the WriteLineActivity will be derived directly from the Activity base class.<br />

The following code shows a constructed activity class <strong>and</strong> defines a Message property that is displayed<br />

when the Execute method is called:<br />

using System;<br />

using System.ComponentModel;<br />

using System.Workflow.ComponentModel;<br />

namespace SimpleActivity<br />

{<br />

/// <br />

/// A simple activity that displays a message to the console when it executes<br />

/// <br />

public class WriteLineActivity: Activity<br />

{<br />

/// <br />

/// Execute the activity — display the message on screen<br />

/// <br />

/// <br />

/// <br />

protected override ActivityExecutionStatus Execute<br />

(ActivityExecutionContext executionContext)<br />

{<br />

Console.WriteLine(Message);<br />

}<br />

return ActivityExecutionStatus.Closed;<br />

/// <br />

/// Get/Set the message displayed to the user<br />

/// <br />

[Description("The message to display")]<br />

[Category("Parameters")]<br />

public string Message<br />

{<br />

get { return _message; }<br />

set { _message = value; }<br />

}<br />

/// <br />

/// Store the message displayed to the user<br />

/// <br />

private string _message;<br />

}<br />

}<br />

Within the Execute method, you can write the message to the console <strong>and</strong> then return a status of Closed to<br />

notify the runtime that the activity has completed.<br />

You can also define attributes on the Message property so that a description <strong>and</strong> category are defined for<br />

that property. This is used in the property grid within Visual Studio, as shown in Figure 57-8.<br />

The code for the activities created in this section is in the 03 CustomActivities solution. If you compile<br />

that solution, you can then add the custom activities to the toolbox within Visual Studio by choosing<br />

the Choose Items menu item from the context menu on the toolbox <strong>and</strong> navigating to the folder where the<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!