15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

We begin with the canonical example that everyone uses when faced with a new technology — “Hello<br />

World” — <strong>and</strong> also describe what you need to get workflows running on your development machine.<br />

hello World<br />

Visual Studio 2010 contains built-in support for creating workflows, <strong>and</strong> when you open the New Project<br />

dialog, you see a list of workflow project types, as shown in Figure 57-1.<br />

figure 57-1<br />

Select Sequential Workflow Console Application from the available templates (that will<br />

create a console application that hosts the workflow runtime) <strong>and</strong> a default workflow onto<br />

which you can then drag <strong>and</strong> drop activities.<br />

Next, drag a Code activity from the toolbox onto the design surface so that you have a<br />

workflow that looks like the one shown in Figure 57-2.<br />

The exclamation mark glyph on the top right of the activity indicates that a<br />

m<strong>and</strong>atory property of that activity has not been defined — in this case, it is the<br />

ExecuteCode property, which indicates the method that will be called when the activity figure 57-2<br />

executes. You learn how to mark your own properties as m<strong>and</strong>atory in the section on<br />

activity validation. If you double-click the code activity, a method will be created for you in the code-behind<br />

class, <strong>and</strong> here you can use Console.WriteLine to output the “Hello World” string, as shown in the<br />

following code snippet:<br />

private void codeActivity1_ExecuteCode(object sender, EventArgs e)<br />

{<br />

Console.WriteLine("Hello World");<br />

}<br />

If you then build <strong>and</strong> run the program, you will see the output text on the console. When the program<br />

executes, an instance of the WorkflowRuntime is created, <strong>and</strong> then an instance of your workflow is<br />

constructed <strong>and</strong> executed. When the code activity executes, it calls the method defined <strong>and</strong> that outputs the<br />

string to the console. The section entitled “The Workflow Runtime” later in the chapter describes in<br />

detail how to host the runtime. The code for the preceding example is available in the 01 HelloWorkflow<br />

World folder.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!