Lập trình .net 4.0 và visual studio 2010 part 20 pptx

10 264 0
Lập trình .net 4.0 và visual studio 2010 part 20 pptx

Đang tải... (xem toàn văn)

Thông tin tài liệu

CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 133 Existing WF3 Users WF has undergone some radical changes from version 3.0, which unfortunately means that for those developers already utilizing WF3 are probably going to have to do some work to do to upgrade their applications. Statemachine Workflow Has Gone Probably the biggest problem you may encounter is in the initial release of WF there is no support for statemachine workflows. Microsoft seems undecided as to whether Statemachine will be introduced in future versions of WF but for now the answer is to utilize the new building blocks. Upgrade White Papers Microsoft has produced a number of excellent documents to assist you in upgrading to WF4, so your first step before you upgrade any applications should be to consult this link: http://go.microsoft.com/ fwlink/?LinkID=153313. WF3 Runtime Version 3 of the WF runtime will be included with .NET 4.0 runtime, so your WF3 should continue to run on the .NET 4.0 framework. Interop Activity WF4 has a new activity called Interop (discussed later in this chapter) that can be used to run legacy WF code. This can serve as a halfway point until you are ready to upgrade your entire application. Is It Worth the Upgrade? Probably. WF4 lays the groundwork for future enhancements, and WF5 should not be such a radical upgrade. WF 4 offers superior performance (10 to 100 times quicker), a much improved designer, and many other changes that we will discuss in this chapter. Kenny Wolf (architect on WCF and WF team) at PDC 08 noted that some customers in the early adopter program reduced their code by 80 percent by moving to WF4. All Change WF4 is a radical change, so even if you are an experienced WF developer please take the time to work your way through this example as the designer has changed completely and a number of new concepts have been introduced. CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 134 Hello WF 4 We will write a simple workflow that will handle a booking for a movie theatre. Our example workflow will do the following: • Process a booking for a specific film • Check if seating is available for the requested showing. In our example, to keep everything simple, if less than five tickets are requested, then the booking will succeed. • Return a Boolean value indicating whether the booking was successful and a hardcoded alphanumeric ticket reference (blank if unsuccessful). Hello WF Open Visual Studio and create a new Workflow Console Application called Chapter6.HelloWF. You should now see a screen similar Figure 6-5. Figure 6-5. New sequential workflow CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 135 The first change that you will notice is that the WF designer like the IDE is rewritten in WPF and is much quicker than the previous version. We are going to be creating a sequential workflow first, so open the toolbox and drag a Sequence activity from the Control Flow group to the designer. Next we will need to supply our workflow with information about the customer's booking. In WF4 arguments and variables are used pass data between activities. This is much easier than in WF3 where dependency properties had to be used. Arguments and Variables Arguments and variables are unique to each running instance of a workflow and hold and return values. 1 They can be of any type and arguments can be used as inputs, outputs, or both input and outputs. The visibility or scope of a variable is dependent on where it is declared. So if a variable is declared inside an activity, it will be visible to child activities, but not to parent activities. As per traditional coding practices you should minimize the exposure of your variables and arguments. Creating an Argument Let’s create a new argument (no not the kind you have with your wife when she is trying to get you off the computer when you are trying to write a book), but a WF4 argument. 1. On the grey bar at the base of the designer window is an Arguments button. Click this. A new section will open on the designer (Figure 6-6). 2. Click where it says Create Argument and add three new arguments with the following settings: • FilmName (type String, direction In). • ShowingDate (type String, direction In). In reality we would pass in a FilmShowingID variable, but this reduces typing of pointless demo code. • NumberOfTickets (type Int32, direction In). 1. Technically arguments and variables don’t actually hold the values, but instead tell the individual workflow how to access the values. CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 136 Figure 6-6. Creating new arguments 3. We will also need two variables to hold whether the booking was successful and the booking reference to return to the user. Click the Variables button at the base of the screen and create two new variables (Figure 6-7): • BookingReference (type String, set the default to “”) • BookingSuccessful (type Boolean) CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 137 Figure 6-7. Creating arguments for our workflow WriteLine WriteLine is an inbuilt activity that allows us to write output to the console window or a text writer. We will use it to write a message to the console window to inform us that the workflow has started. 1. Drag a WriteLine activity from the toolbox (Primitives section) onto the Sequence activity, placing it on the grey arrow in the white box. 2. Click the WriteLine activity then in the Properties window change the Display Name to “Workflow started.” Display Name is a property present on most activities, and is simply a label for the activity. Display Name is also used for constructing a breadcrumb trail in the IDE when we drill down into child activities, so it is good practice to set it to something useful. Finally, change the text property to “Workflow started” (include the quotes).  NOTE When writing out values in expressions WF uses VB.NET expression syntax, and requires you to contain values such as text property within quotes. I use quotes in this tutorial to make the text easier to pick out, but you should not enter the quote characters when setting properties such as Display Name. CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 138 3. Click the Sequence activity and change the display name to “Book Film.” Your workflow should be looking like Figure 6-8. Figure 6-8. Creating our workflow 4. We want to see the output of our workflow before it closes, so open Program.cs and add the following code at the end of the Main() method: Console.ReadLine(); 5. Press F5 to run your workflow and you should see “Workflow started” outputted to the screen. Congratulations! You have created your first workflow application. Creating Another Sequence Activity The next step in our contrived example is to check if seating is available for a specific film showing. We will create a Sequence activity that contains a number of other activities to perform this check. 1. Drag a Sequence activity below the WriteLine activity. 2. Change the display name to “Check availability.” 3. To drill down into an individual activity, you double-click on it. So double-click the “Check availability” sequential activity we have just created and you should see a screen similar to Figure 6-9. CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 139 Figure 6-9. Drilled down view of Sequential activity As we are “inside” one of the activities you probably want to know how to get back to the level above. There are two ways of doing this: • Right-click on the design surface and select the View Parent option. • Use the breadcrumb trail that is automatically constructed at the top of the page. Note how the activity's display name is used to form the breadcrumb trail. This is a very good reason to set it to something descriptive. Individual activities can be collapsed and expanded to improve your view of the workflow Checking Number of Tickets with an If Activity We now need to check if the number of tickets requested is more than five, and in our example we will prevent such a booking. 1. Drag an If activity onto the designer (in the "Check availability" activity). 2. Change the display name to “If NumberOfTickets>5.” 3. Double click the If activity. 4. In the Properties window, select the Condition property and click the ellipsis button. 5. Enter the following in the Expression Editor window: NumberOfTickets > 5  WARNING Expressions are written in VB syntax. Expressions must be entered in VB syntax whether you are creating a VB or C# workflow project. The WF team claim that VB is more intuitive (ahem) and that some power users will be used to VBA syntax from a product such as Excel. CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 140 Booking Unsuccessful and Assign activity The Assign activity allows us to assign values to variables. We will use it to update the BookingSuccessful variable. 1. Drag an Assign activity from the Primitives group into the Then box of the If activity. 2. Change the display name to “Booking unsuccessful.” 3. Change the To property to the BookingSuccessful variable. 4. Change the Value property to False. Booking Succssful and Parallel Activity We want to set the BookingSuccessful property to True and also set a booking reference. The Parallel activity allows us to perform two activities at the same time (OK, this is unnecessary for this situation but I wanted to make you aware of this cool feature). 1. Drag a Parallel activity from the Control Flow group to the Else segment of the If activity. 2. Change the display name to “Booking successful.” Your If activity should look like Figure 6-10. Figure 6-10. If statement 3. Double-click the “Booking successful” activity. 4. Drag an Assign activity onto the , and change its display name to “Assign Booking successful.” 5. Change the To property to the BookingSuccessful variable. 6. Change the Value property to True. 7. Drag another Assign activity onto the arrows to the right of the original Assign activity and change the display name to “Get booking ref.” CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 141 8. Change the To property to BookingReference. 9. Change the Value property to "abcde" (we will hard code this value in our example, and note that the quotes are needed). Your Parallel activity should look like Figure 6-11. Figure 6-11. Parallel activity allows you to specify that tasks should occur at the same time. Displaying the Output of the Booking We will now use another WriteLine activity to display the output of our workflow. 1. Click Book Film in the breadcrumb trail to return to the top view. 2. Drag another WriteLine activity to the end of the workflow and change the display name to “Display output.” 3. On the Text property click the ellipsis and enter the following: "Booking result: " + BookingSuccessful.ToString() + " Reference: " + BookingReference.ToString() Supplying Arguments to a Workflow The only thing left to do is supply our workflow with some initial arguments. As per previous versions of WF, arguments are supplied as a dictionary variable.  TIP Names are case sensitive and magic strings are bad, so in real-world applications you will probably want to create a wrapper class instead of using the below approach. A common approach is to inherit from the Dictionary class and create properties to set values within it. CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 142 4. Open Program.cs and add the following using statement: using System.Collections.Generic; 5. Modify the Main() method to the following to create and pass in a dictionary of our initial variables: static void Main(string[] args) { Dictionary<string, object> Arguments = new Dictionary<string, object>(); Arguments.Add("FilmName", "Terminator"); Arguments.Add("ShowingDate", System.DateTime.Now.ToString()); Arguments.Add("NumberOfTickets", 4); WorkflowInvoker.Invoke(new Workflow1(), Arguments); Console.ReadLine(); } 6. That’s it; you have just created your first workflow. Now press F5 to run it. 7. Try changing the NumberOfTickets to five. Do you get a different result? Wow, we covered a lot in that short example. You discovered the new workflow designer, how to use arguments and variables, and some of the new activities in WF4. Creating Your Own Activities Microsoft supplies many activities out of the box, but you will want to and should create your own activities. Activities can be created in three main ways: • As a composition of other existing activities • In code • Pure XAML Creating an Activity Composed of Other Activities Let’s create a simple activity to simulate writing a customer’s ticket booking to a database. 1. Right-click on the project and add a new activity (from the Workflow section) called SaveBooking.xaml. 2. Open SaveBooking.xaml in the design view. 3. Create an argument for the SaveBooking activity (String, In) called BookingReference. 4. Drag a Sequence activity onto the design surface. 5. Drag a WriteLine activity onto this activity and change its display name to “Simulate booking storage.” . Version 3 of the WF runtime will be included with .NET 4. 0 runtime, so your WF3 should continue to run on the .NET 4. 0 framework. Interop Activity WF4 has a new activity called Interop (discussed. Worth the Upgrade? Probably. WF4 lays the groundwork for future enhancements, and WF5 should not be such a radical upgrade. WF 4 offers superior performance ( 10 to 100 times quicker), a much improved. (architect on WCF and WF team) at PDC 08 noted that some customers in the early adopter program reduced their code by 80 percent by moving to WF4. All Change WF4 is a radical change, so even if

Ngày đăng: 01/07/2014, 21:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan