WF4教程第一彈

來源:互聯網
上載者:User

本文主要是總結自己學習《microsoft windows workflow foundation 4.0 cookbook》第一章的內容。下面給出本章程式的原始碼,如下:Chapter01.rar。下面簡要介紹解決方案Chapter01中所有項目。解決方案中的各個項目如所示

1.Creating the first WF program:HelloWorkflow

HelloWorkflow is a Workflow 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 “查看代碼” 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是一個“工作流程控制台應用程式”,我們在工作流程設計工具中建立的工作流程其實就是一個XML檔案,在VS2010中我們可以通過以下方法查看工作流程的XML檔案:右鍵Workflow1.xaml,選擇“查看代碼”。到目前為止,只有VS2010有WF4的設計器,VS2008中還沒有WF4編輯器的外掛程式,因此如果要在VS2008中開發WF4,那麼就只能通過編寫純XAML文檔的方式進行。

2.Creating a WF program using C# Code

HelloCodeWorkflow is a Console Application.這個項目是通過純程式碼的方式來建立WF4項目。具體見代碼,也可以參考書本。

3.Initializing a WF program using InArguments

UseInArgument is a Workflow Console Application.在這個項目中主要是建立了InArgument類型的參數。可以為輸入參數傳遞變數。

4.Creating a WF program using OutArgument

UseOutArgument is a Workflow Console Application.OutArgument是輸出參數類型,在工作流程中可以作為輸出參數用。在宿主應用程式中可以通過特定的方法輸出該參數中的內容。

The WorkflowInvoder.Invoke method will return a IDictionary type object.

WorkflowInvoder.Invoke方法會返回IDictionary類型對象。

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 use InOutArgument instead of InArgument and OutArgument. But there are still some differences—for example, we cannot assign a string to InOutArgument, while it is allowed to

assign a string to InArgument directly in the host program.

在大多數情況下,可以用InOutArgument參數類型來代替InArgumentOutArgument但是也有一些例外情況,比如我們可以直接將一個string類型的變數賦值為InArgument類型的參數,但是卻不能在宿主程式中直接將string字串賦值給InOutArgument類型的參數。

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).

InOutArgument變數既可以用來擷取值,也可以用來向WF宿主輸出值。

UseInOutArgument is a Workflow Console Application.

6.Using Variable in a WF program

UseVariable is Workflow 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.

在WF程式運行期間,我們可以通過變數(Variable來暫時儲存一個值。

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

UseWorkflowApplication a Workflow 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 should run on an independent thread. In this task, we will use

WorkflowApplication to run a workflow instance.

通過使用WorkflowInvoker.Invoke方法來啟動一個工作流程執行個體。工作流程和宿主程式是在同一個線程上運行。這樣用起來雖然方便,當時在大多數實際的應用程式中,都是將宿主程式與工作流程序的線程分開。讓工作流程在一個獨立的線程中運行。

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.

如果工作流程線程和調用者線程同時運行,調用者線程可能在工作流程線程之前結束 為了防止這種意想不到的程式退出,我們需要通過使用AutoResetEvent來使得 調用者工作流程線程非同步

8.Customizing a MyReadLine activity with Bookmark

MyReadLineActivity a Workflow 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.

可以通過參數InArgument, OutArgument, and InOutArgument來使得資料在工作流程中流動。但是我們如何在工作流程執行期間從調用程式向工作流程傳遞資料呢。Bookmark可以協助我們完成這樣的任務。

In WF4, workflow is actually an Activity class. We could see "Workflow" as a conception from a macroeconomic viewpoint, while considering "Activity" as a development concept.

在WF4中,工作流程實際上就是一個Activity類。我們可以將工作流程理解為宏觀上的概念,而活動則是開發上的概念。

9.Converting a WF program instance to XAML

ConvertWFInstanceToXML a Workflow Console Application.

In real applications, we would 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.

將工作流程序執行個體轉換成XAML類型的字串。

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

LoadUpWorkflowFromXML a Workflow Console Application.

In this task, we will run a WF program by loading it from an XAML file.

在本執行個體中,通過載入XAML檔案來運行工作流程應用程式。

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]表示這個一個單元測試類。[TestMethod]表示這是一個測試類別。當測試專案啟動並執行時候,測試方法會自動執行。

12.Debugging a WF program

DebugWFProgram a Workflow Console Application.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.