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 7 pps
PREMIUM
Số trang
57
Kích thước
962.6 KB
Định dạng
PDF
Lượt xem
723

microsoft press windows workflow foundation step by step phần 7 pps

Nội dung xem thử

Mô tả chi tiết

Chapter 10 Event Activities 215

Figure 10-2 Adding a new watched stock

Selecting a symbol in the ticker symbol list enables the Remove button. Clicking the Remove

button removes the item from the watched stock list. The removal action is shown in Figure

10-3. The stocks you are monitoring are stored in the application’s Settings file (in XML form).

The next time you execute eBroker, it will “remember” your stocks and begin checking anew.

Figure 10-3 Removing an existing watched stock

In Figure 10-2, you see that the application needs to know how many shares you currently

have so that it can calculate the total value of the shares you own. These figures are used to cal￾culate the current market value. If you later want to adjust the number of shares (by buying

and selling stock), select the stock in the market value list and click either Buy! or Sell! The

dialog box you see in Figure 10-4 will request the number of shares you want to buy or sell,

and the workflow will be notified.

Figure 10-4 Dialog box requesting number of shares to buy or sell

The Add dialog box in Figure 10-2 also requests buy and sell “trigger” amounts. The workflow

contains business logic that uses these values to notify you when not you should buy or sell

shares in any of the companies you are currently monitoring. If the stock price exceeds the sell

trigger value, a red flag is displayed in the market list. If the stock price drops below the buy

trigger value, a green flag appears. You can buy and sell shares at any time...the flags are just

visual indicators. You see a couple of flags in Figure 10-5.

216 Part II Working with Activities

Figure 10-5 The eBroker user interface indicating buy/sell recommendations

Note DO NOT think for a microsecond that the simulation I’ve provided here in any way

truly simulates any stock market or company on the planet. The simulation is completely fab￾ricated for demonstration purposes only.

Each of these four buttons (Add, Remove, Buy! and Sell!) fires an event to the workflow, pro￾viding the appropriate content, which includes the watched stock to add or remove and the

number of shares to buy or sell (so that the total market value is accurate). There is a fifth

event, Stop, that is used to stop the simulation from executing. This event is fired by the Quit

button.

Much of the application has been written for you, allowing you to concentrate on the

workflow-related aspects. Here is the bigger picture. First, you’ll complete the interface

the workflow and host will use for communication. Then you’ll use wca.exe to create activities

based on the CallExternalMethod activity and the HandleExternalEvent activity. With these in

hand, you’ll lay out the actual workflow, using each of the activities you’ve seen in this chapter.

You’ll see how the local communication service glues the host application and workflow com￾munication process together. And finally, you’ll briefly examine and add code to the eBroker

user interface source file to direct it to interact with the workflow. Let’s get started!

Creating the Communication Interface

We need a single method, MarketUpdate, to return market information to the user interface,

as well as five events. The events—AddTicker, RemoveTicker, BuyStock, SellStock, and Stop—are

used to drive the workflow. The single method and five events are plugged into an interface,

which we’ll build first. Everything related to the local communications service hinges on

this interface.

Creating a workflow data communication interface

1. Open Visual Studio, and open the eBroker application’s solution from the book samples.

You’ll find the solution in \Workflow\Chapter10\. Click File, Open, and then finally

Chapter 10 Event Activities 217

Project/Solution. Using the resulting Open Project dialog box, browse your computer’s

file system until you find this chapter’s sample and open its solution file.

Note As with the most sample applications in this book, the eBroker sample applica￾tion comes in two forms: incomplete and complete. You can follow along and add

code to the incomplete version, or you can open the complete version and verify that

the code I mention here is in place.

2. You will find that three projects have been added to the solution. In Visual Studio

Solution Explorer, expand the eBrokerService project and open the IWFBroker.cs file for

editing.

3. Locate the namespace definition. After the opening brace for the eBrokerService

namespace, add this code and then save the file:

[ExternalDataExchange]

public interface IWFBroker

{

void MarketUpdate(string xmlMarketValues);

event EventHandler<TickerActionEventArgs> AddTicker;

event EventHandler<TickerActionEventArgs> RemoveTicker;

event EventHandler<SharesActionEventArgs> BuyStock;

event EventHandler<SharesActionEventArgs> SellStock;

event EventHandler<StopActionEventArgs> Stop;

}

4. Compile the project by pressing Shift+F6 or by selecting Build eBrokerService from the

main Visual Studio Build menu. Correct compilation errors, if any.

Don’t forget the ExternalDataExchange attribute. Without it you cannot successfully transfer

information between workflow and host using the data transfer mechanism I describe here.

Before you create the communication activities (using wca.exe), take a moment to open

and glance through the event arguments you see in the eBrokerService project.

MarketUpdateEventArgs is really no more than a strongly typed version of

System.Workflow.ExternalDataEventArgs, as is StopActionEventArgs. These event argument

classes convey no data. However, TickerActionEventArgs and SharesActionEventArgs both

convey information to the workflow. TickerActionEventArgs carries XML representing the

stock to add or remove, while SharesActionEventArgs carries the ticker symbol as a primary

key, as well as the number of shares to buy or sell.

Tip Designing the event arguments is important because the event arguments carry data

from the host to the workflow. Moreover, wca.exe examines the event arguments and builds

bindings into the derived classes that allow you to access the data from the event arguments

as if the data were intrinsic to the derived activity. Put another way, if the event argument has

a property named OrderNumber, the class that wca.exe builds would have a property named

OrderNumber. Its value would come from the underlying event’s event argument and would

be assigned automatically for you.

218 Part II Working with Activities

Now let’s use wca.exe to create the communication activities.

Creating the communication activities

1. To begin, click the Start button and then the Run button to activate the Run dialog box.

Windows Vista users who have not installed the Run command on the Start button can

run the command prompt by selecting the All Programs button from the Start menu.

When the programs appear, select Accessories and then Command Prompt.

2. Type cmd in the Open combo box control, and click OK. This activates the Command

Shell.

3. Change directories so that you can directly access the eBrokerService assembly in

the book’s downloaded sample code. Typically, the command to type is as follows:

cd "\Workflow\Chapter10\eBroker\eBrokerService\bin\Debug". However, your

specific directory might vary.

4. As you did in Chapter 8, type the following at the command-line prompt (including

the double quotes): "<%Program Files%>\Microsoft SDKs\Windows\v6.0

\Bin\Wca.exe" /n:eBrokerFlow eBrokerService.dll. (Note that <%Program Files%>

represents the location of your Program Files directory, typically "C:\Program Files".)

Press the Enter key.

5. wca.exe loads the assembly it finds in eBrokerService.dll and scans the interfaces it

locates for one decorated with the ExternalDataExchange attribute, which in this case

is IWFBroker. The methods are parsed out and turned into classes derived from the

CallExternalMethod activity and stored in the file named IWFBroker.Invokes.cs. The

events are similarly turned into classes derived from the HandleExternalEvent activity

and placed in IWFBroker.Sinks.cs. Rename the “invokes” file by typing this command

on the command line: ren IWFBroker.Invokes.cs ExternalMethodActivities.cs.

6. Rename the “sinks” file by typing the following at the command-line prompt:

ren IWFBroker.Sinks.cs ExternalEventHandlers.cs.

7. Move both files from the current directory into the workflow project’s directory using

this command: move External*.cs ..\..\..\eBrokerFlow.

8. Now return to Visual Studio and add the newly created files to the eBrokerFlow work￾flow project. Right-click the eBrokerFlow project name in Solution Explorer and select

Add, then Existing Item. From the resulting Add Existing Item dialog box, select the

files to add and then click Add. Be sure to add both ExternalMethodActivities.cs and

ExternalEventHandlers.cs to the project.

9. Compile the eBrokerFlow project by pressing Shift+F6, and fix any compilation errors

that might have occurred. Once you have a successful compilation, verify that Visual

Studio placed the custom activities (in the C# files you just loaded) into the Toolbox. To

do this, open Workflow1.cs for editing in the visual designer by selecting the file and

then clicking the View Designer toolbar button in Solution Explorer. With the workflow

Chapter 10 Event Activities 219

loaded into the visual workflow designer, open the Toolbox and look for the custom

events. You should find AddTicker, BuyStock, and so forth loaded at the head of the Tool￾box tool list.

Note As a reminder, if after compiling the workflow solution the new activities don’t

appear in the Toolbox, closing and then opening the eBroker solution will force them

to load. You’ll need them in the next section.

Creating the broker workflow

1. In the eBrokerFlow project, if you didn’t already open Workflow1.cs for editing in the

visual designer, do so now. Select the file in Solution Explorer and click the View

Designer button on the toolbar.

2. To begin the workflow, insert a Code activity that will be used to assign the desired time

duration to a Delay activity (which you’ll insert later), as well as to initialize some internal

data structures. Drag an instance of the Code activity onto the visual workflow designer’s

surface. Once you’ve done this, type Initialize into the ExecuteCode property to create

the Initialize event handler in the workflow code. After Visual Studio inserts the event

handler, return to the workflow visual designer to continue adding activities.

Note Although the user interface doesn’t allow you to change this, there is a setting

for a delay in the workflow that governs the Delay activity in this step. This delay rep￾resents the time elapsed between successive stock value queries. In reality, you’d not

check more often than every 15 or 20 minutes, if that often. But to see the values

actually shift and change, the delay between the simulated stock-market value checks

is set to 7 seconds. The delay value is stored in Settings.

3. Next drag an instance of EventHandlingScope onto the visual workflow designer’s surface

and drop it.

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 7 pps | Siêu Thị PDF