Tải bản đầy đủ (.pdf) (6 trang)

Lập trình .net 4.0 và visual studio 2010 part 19 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (291.46 KB, 6 trang )

CHAPTER 6

  

127
Windows Workflow
Foundation 4
Availability: Framework 4.0
Windows Workflow Foundation (WF) was first introduced in 2006 with .NET 3.0. It is probably fair to say
that WF didn’t receive the widespread adoption Microsoft was hoping for. This lack of uptake was
probably due to a number of factors:
• Although the WF designer offers a natural way of working, it is a very different way
of developing applications and contains a new API to master.
• Slow performance.
• Writing your own work flow activities was not as easy as it could be.
• Handling and passing data between activities was cumbersome
• Limited support for messaging scenarios and integration with WCF.
• Some developers were confused by the hosting model.
• A clunky designer interface made you want to poke your own eyes out (OK, it
wasn’t that bad but it wasn’t that good either).
Microsoft has aimed to address these issues and also to integrate WCF and WF closer than ever
before.
Why Use WF?
Before we look at how to use Microsoft WF you may be wondering why should I bother?
WF can greatly simplify the development of applications that deal with complex and long-running
Processes as traditional coding methods are not ideal for solving this type of problem.
To understand why they are not, let’s take the example of a visa approval system (loosely based on
my experience of the UK/Australian emigration process) as shown in the flowchart in Figure 6-1.


CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4


128

Figure 6-1. Visa processing system workflow
CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4
129
Even if you have no experience of the Australian emigration process you can probably gain an
understanding from Figure 6-1. Windows Workflow enables you to design and develop your application
in a similar visual manner. Consider how an application to handle such a process would be coded: it
would probably be difficult to gain an understanding at a glance. Figure 6-2 shows a possible WF
implementation of the process. As you can see it’s not so different from Figure 6-1.

Figure 6-2. Possible WF implementation of above process
Now let’s consider some of the issues we may encounter when developing this application:
• The client wants to process visa applications as quickly as possible, so he wants to
alert staff when an application is ready for the next stage. How will you provide this
functionality in an efficient and timely manner when processing hundreds of
thousands of application?
• How will you pass data between different services and government departments,
such as performing a police check on an applicant?
• Some tasks take a long time to complete or require human intervention before they
can proceed. How will you handle this waiting?
• A typical visa will take weeks or months to process. How are you going to store the
current state of a visa application?
CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4
130
• What will happen to an application in the event of system failure?
• How will you design your application so that it is flexible enough to accommodate
future changes?
• Imagine you need to debug and monitor different stages of the approval process;
how will you do this?

Agggrahh! As you can see even a simple process can get very complex quickly. Of course you can
develop such an application very successfully (and many have) using current technologies, but Windows
Workflow has many inbuilt features to handle some of this complexity.
What Can Windows Workflow Do for Me?
Windows Workflow provides the following functionality out of the box:
• A visual method of constructing your applications similar to our flowchart in
Figure 6-1. This visual approach is more intuitive, can be easier to debug , is easier
to unit test with changes in this release (a controversial idea, but discussed in the
following sections), and can be understood by non-technical users. Of course
developing an application entirely in code has its advantages as well — it is
generally easier to test, and developers won’t have to learn new ways of writing
applications.
• More efficient use of server resources. Non-active workflows “sleep” and are
“rehydrated” when needed.
• Coordination and synchronization. Workflows that make calls to external services
may take weeks to receive a reply. By using correlation, we can ensure returned
calls are automatically directed to the correct instance of a work flow that can then
“wake up” and resume its work
• Workflow state can be persisted even during system down time and resumed
automatically.
• Ability to host the workflow designer within your applications for customization
and configuration by end users.
• Rich debugging and monitoring support.
• A common framework for workflow development on the Windows platform. WF is
already utilized in flagship products such as Microsoft Sharepoint and Dynamics
(note these use WF3 at time of writing). You can even host your workflows in
Microsofts cloud computing platform Windows Azure (at the time of writing this is
not available, but should be in the future).
• WF can assist you with versioning and updating issues (although for the forseeable
future this is never going to be that easy).

Hopefully I have convinced you that Workflow is something that you should be interested in. Let’s
take a closer look.
CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4
131
What Is Windows Workflow Foundation?
Windows Workflow Foundation is composed of 3 main components:
• Workflow instance/runtime
• Activities
• Workflow designer
Workflow Instance/Runtime
The Workflow runtime takes care of instantiating individual workflows and handles persistence and
synchronization. The workflow runtime does not exist on its own. It is created inside a host application.
The host application can be anything from an ASP.NET website to a windows service or even the new
“Dublin” or Windows Azure platforms.
In previous versions of WF the WorkflowRuntime class was restricted to one WorkflowRuntime per app
domain and WorkflowInstance instantiated individual workflows. WorkflowInstance has now been
replaced with the class WorkflowInstance, which represents an individual workflow.
This change makes it much easier to attach event handlers and delegates to individual workflows,
but also arguably complicates tasks such as recording errors as they now have to be dealt with on an
individual workflow basis.
Activities
Workflows are made up of activities. Microsoft provides a number of building block activities providing
basic logic and flow statements for you to use called the Base Activity Library (BAL). You can of course
(and should) create your very own activities. Figure 6-3 shows the WF activity hierarchy.

Figure 6-3. WF activity class hierarchy
Activities can be created as composites of existing activities, entirely in code or even just using
XAML. Activities inherit from the class Activity (unlike WF3 that used SequentialWorkflowActivity or
StateMachineWorkflowActivity). We will look at creating customized activities shortly.
CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4

132
Workflow Designer
The designer (Figure 6-4) is used to piece together your workflow, and contains tools for debugging and
monitoring it.

Figure 6-4. Workflow designer
It is even possible to host the workflow designer within your application. For more information on
this please refer to the following links:

• />Hosted-Designer/
The trident project (a tool for customizing scientific workflows) used this facility extensively. Take a
look at:

×