Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

microsoft press windows workflow foundation step by step phần 2 doc
PREMIUM
Số trang
47
Kích thước
727.8 KB
Định dạng
PDF
Lượt xem
893

microsoft press windows workflow foundation step by step phần 2 doc

Nội dung xem thử

Mô tả chi tiết

23

Chapter 2

The Workflow Runtime

After completing this chapter, you will be able to:

■ Be able to host the workflow runtime in your applications

■ Understand the basic capabilities of the WorkflowRuntime object

■ Know how to start and stop the workflow runtime

■ Be able to connect to the various workflow runtime events

When you execute tasks in the Workflow Foundation (WF) environment, something needs to

oversee that execution and keep things straight. In WF, that something is an object known as

WorkflowRuntime. WorkflowRuntime starts individual workflow tasks. WorkflowRuntime fires

events for different situations that pop up while your tasks execute. And WorkflowRuntime

keeps track of and uses pluggable services you can hook in to the execution environment.

(We’ll look at some of these pluggable services starting in Chapter 5, “Workflow Tracking.”)

The overall WF architecture is shown in Figure 2-1.

Figure 2-1 WF architecture

Host Application

Activity

Activity Activity Activity

Activity

Threading

Services

Persistence

Services

Timer

Services

Custom

Services

Tracking

Services

External Data

Services

Transaction

Services

Managed

Application Code

Workflow Environment

WorkflowRuntime

WorkflowRuntime

Workflow Instance

AppDomain

24 Part I Introducing Windows Workflow Foundation (WF)

WF and your application execute concurrently. In fact, WF requires your application as a host.

The host application might be a Windows Forms application, a console application, an

ASP.NET Web application, or even a Windows service. The WF runtime and your application

execute together in a .NET AppDomain, and there can be only one instance of

WorkflowRuntime per AppDomain. Attempting to create a second instance of WorkflowRuntime

in a single AppDomain results in an InvalidOperationException.

You build workflow applications—“workflows”—by creating logical groupings of activities.

These logical groupings work to complete the workflow task you require. When you host the

workflow runtime, you essentially hand the workflow your activities and tell it to execute

them. This results in a workflow instance. The workflow instance is a currently executing

workflow task, which is itself composed of logically grouped activities. And, as you recall from

the first chapter, activities can execute code you provide as well as make decisions based on

input data. We’ll cover workflow instances in the next chapter and activities in the chapters

to follow.

Hosting WF in Your Applications

In the last chapter, we used the Microsoft Visual Studio workflow project template to build a

basic workflow application for us. And in practice you would likely do just that. But if you’re

like me, just executing wizards and such is fine only if you understand the code they’re insert￾ing. After all, the code is yours to maintain and understand once the code generator’s job is

complete.

So what does it take to host WF in your application? Well, aside from building the workflow

tasks that WF is to run (that’s your job), all you really need to do is reference the WF assem￾blies and provide the necessary code to bring WorkflowRuntime into execution, start it, and

manage the operational conditions you’re interested in managing. In that sense, hosting WF

isn’t a lot different from using other .NET assemblies. The operational condition management

amounts to handling events that the runtime will fire from time to time given specific condi￾tions, such as when the runtime goes idle or an instance sustains an unhandled exception.

There is quite a list of available events you can handle, and we’ll see some of those a bit later

in the chapter, with still others introduced in Chapter 5, “Workflow Tracking,” and Chapter 6,

“Loading and Unloading Instances.”

Note WF can be hosted in a variety of applications, including Microsoft Windows Forms

and Windows Presentation Foundation applications, console applications, ASP.NET Web

applications, and Windows Services. The basic process remains the same as far as WF is

concerned for all of these (very different) host application types.

For now, though, let’s build a basic .NET console application and host the workflow runtime

ourselves. This will help make the code the Visual Studio workflow project template inserts a

little less mysterious.

Chapter 2 The Workflow Runtime 25

Creating a basic console application

1. Start Visual Studio 2005 as you did in the previous chapter.

2. From the File menu, select New and then Project.

3. When the New Project dialog box appears, expand the Visual C# tree control node and

then select Windows from the Project Types pane.

4. Select Console Application from the Templates pane.

5. In the Name field, type WorkflowHost.

6. In the Location field, type \Workflow\Chapter2.

Note Remember that the path \Workflow represents the path you are using to store

the book’s sample applications.

7. Click OK to create the WorkflowHost project.

At this point, we have a basic console application, but of course it does nothing interesting.

Now let’s begin adding workflow components. Speaking personally, I truly love the Visual

Studio IntelliSense functionality. But for that to take effect, you have to first reference the

assemblies IntelliSense will interpret to help you write code. So a great place to start is to

reference the workflow assemblies before adding any code. This way, when we do add code,

we can take advantage of the Visual Studio code assistance capabilities.

Adding the workflow assembly references

1. In the Visual Studio Solution Explorer pane, right-click the References tree node and

select Add Reference.

Tip Selecting Add Reference from the Visual Studio Project menu achieves the same

result.

26 Part I Introducing Windows Workflow Foundation (WF)

2. This activates the Add Reference dialog box. Using the vertical scrollbar’s thumb control,

scroll down until you find System.Workflow.Runtime. Select that using a single mouse

click.

3. Click OK to add the reference.

Visual Studio has now added the workflow runtime reference you’ll need to execute workflow

tasks. What we’ve not done is actually bring the workflow runtime into execution. To do that,

we need to add some code to our application—here’s what we’ll do.

Hosting the workflow runtime

1. If it’s not already open, open the Program.cs file for editing as you did in the previous

chapter.

2. Locate the following code (which is located at the top of the source file):

using System;

using System.Collections.Generic;

using System.Text;

3. Add the following line of code, just after the System.Text line:

using System.Workflow.Runtime;

4. Locate the Main method, and add the following line of code after the opening curly

brace:

WorkflowRuntime workflowRuntime = new WorkflowRuntime();

5. For now, we’ll just compile the program to make sure there are no errors. We’ll use this

application throughout the chapter, so keep Visual Studio running, or reload this appli￾cation as necessary while progressing through the chapter. To compile, select Build

WorkflowHost from the Visual Studio Build menu.

Tải ngay đi em, còn do dự, trời tối mất!
microsoft press windows workflow foundation step by step phần 2 doc | Siêu Thị PDF