This article summarizes the first chapter of Microsoft Windows Workflow Foundation 4.0 cookbook. This chapterProgramOfSource code, As follows: chapter01.rar. The following describes all the projects in solution chapter01. Shows the projects in the solution.
1. Creating the first WF program: helloworkflow
Helloworkflow isWorkflow console application. The workflow we created in WF designer is actually an XML file. We can open workflow1.xaml with an XML editor to check it.
In Visual Studio 2010 we can right-click on workflow1.xaml then click "ViewCode"Open workflow1.xaml as an XML file.
So far, there are no officially published wf4 designer add-ins for Visual Studio 2008. we need a copy of Visual Studio 2010 installed on our computer to use wf4 designer, otherwise we can only create workflows by imperative Code or by writing pure XAML files.
Helloworkflow is a "workflow console application". The workflow we create in the workflow designer is actually an XML file. In vs2010, we can view the XML file of the workflow using the following methods: right-click workflow1.xaml and choose view code ". So far, only vs2010 has the wf4 designer, and vs2008 does not have the wf4 editor plug-in. Therefore, if you want to develop wf4 in vs2008, therefore, you can only write a pure XAML document.
2. Creating a WF program using C # code
Hellocodeworkflow isConsole application.This project creates a wf4 project using pure code. For details, see the code or reference books.
3. initializing a WF program using inarguments
Useinargument isWorkflow console application.In this project, the inargument type parameters are created. You can pass variables for input parameters.
4. Creating a WF program using outargument
Useoutargument isWorkflow console application.Outargument is an output parameter type and can be used as an output parameter in a workflow. The content of this parameter can be output in a specific method in the Host application.
TheWorkflowinvoder. InvokeMethod will returnIdictionaryType object.
The workflowinvoder. Invoke method returns an idictionary type object.
There is a third type of workflow argument:Inoutargument. It is a binding terminal that represents the flow of data into and out of an activity. In most cases, we can useInoutargumentInsteadInargumentAndOutargument. But there are still some differences-for example, we cannot assign a stringInoutargument, While it is allowed
Assign a string to inargument directlyIn the host Program.
In most casesInoutargumentParameter typeTo replaceInargumentAndOutargument.However, there are some exceptions. For example, we can directly assign a variable of the string type to a parameter of the inargument type, however, the string cannot be directly assigned to an inoutargument parameter in the Host Program.
5. Creating a WF program using inoutargument
In this task, we will create a WF program using inoutargument. This type of argument is used to receive values and is also used to pass values out to the caller (WF host ).
The inoutargument variable can be used to obtain values or output values to the WF host.
Useinoutargument isWorkflow console application.
6. Using Variable in a WF Program
Usevariable isWorkflow console application.
We can use variable temporarily to store a value when a WF program is running. in this task, we will create a WF program that prints five numbers to the console in a loop. we will use the numbercounter variable as a number counter.
During the WF program running, we can useVariable (Variable)To save a value temporarily.
While we can use arguments to flow data into and out of a workflow, we use variable to store data in a workflow. every variable has its scope, and can be accessed by activities within its scope. variable in wf4 is pretty much like variables in imperative language such as C #.
7. Running a WF program Asynchronously
UseworkflowapplicationWorkflow console application.
In the previous tasks, we used the workflowinvoker. Invoke method to start a workflow instance on the same thread as the main program. It is easy to use; however, in most real
Applications, a workflow shocould run on an independent thread. In this task, we will use
Workflowapplication to run a workflow instance.
Start a workflow instance by using workflowinvoker. invoke. The workflow and the host program run on the same thread. This makes it easy to use. In most actual applications, the host Program and the workflow program thread were separated. Let the workflow run in an independent thread.
As the workflow thread runs simultaneously with the caller thread, the caller thread may terminate before the workflow thread. To prevent this unexpected program quit, we need to use autoresetevent to synchronize caller and workflow thread.
If the workflow thread and the caller thread run simultaneously, the caller thread may end before the workflow thread to prevent unexpected program exit. We need to use autoresetevent to make the caller workflow thread asynchronous.
8. customizing a myreadline activity with bookmark
MyreadlineactivityWorkflow console application.
By using, We can flow data into the workflow when it starts and out of the workflow when it ends. But how can we pass data from the caller into the workflow when it is executing? -Bookmark will help us to do this. In this task, we will create a myreadline activity using a bookmark.
The inargument, outargument, and inoutargument parameters can be used to flow data in the workflow. But how can we transfer data from the calling program to the workflow during workflow execution. Bookmark can help us complete such a task.
In wf4, workflow is actually an activity class. We cocould see "workflow" as a conception from a macroeconomical viewpoint, while considering "activity" as a development concept.
In wf4, a workflow is actually an activity class. We can understand workflows as macro concepts, while activities are development concepts.
9. Converting a WF program instance to XAML
ConvertwfinstancetoxmlWorkflow console application.
In real applications, we wowould like to write and test WF programs in imperative code, while storing, running, and transmitting workflow as an XAML string or file. in this task, we will convert a WF program instance to an XAML string.
Converts a workflow program instance to a string of The XAML type.
Consider the following code line:
Xamlservices. Save (XW, AB );
Xamlservices provides services for the common XAML tasks of reading XAML and writing an object graph, or reading an object and writing out an XAML file. this statement reads an activitybuilder object and writes XAML to an xamlwriter object.
We use activitybuilder as an activity wrapper so that the output XAML is a loadable workflow. in other words, if we save, say, a sequence activity to an xamlwriter directly, then the output XML workflow will be unloadable for further use.
10. Loading up a WF program from an XAML File
LoadupworkflowfromxmlWorkflow console application.
In this task, we will run a WF program by loading it from an XAML file.
In this example, you can load the XAML file to run the workflow application.
11. Testing a WF program with a unit test framework
Unittestforwfprogram is a test project.
[Testclass] indicates it is a unit test class, whereas [testmethod] indicates a test method. When the test project runs, the test method will be executed automatically.
[Testclass] indicates this unit test class. [Testmethod] indicates that this is a test class. When the test project is running, the test method is automatically executed.
12. debugging a WF Program
DebugwfprogramWorkflow console application.