Book Image

Microsoft Windows Workflow Foundation 4.0 Cookbook

By : Andrew Zhu
Book Image

Microsoft Windows Workflow Foundation 4.0 Cookbook

By: Andrew Zhu

Overview of this book

Windows Workflow Foundation 4.0 (WF) is a significant part of .NET Framework 4.0. WF makes workflow technology available to every single programmer that uses the .NET Framework 4.0. It is easy to create long running and distributed programs using WF with the right knowledge. With this book, you will discover that working with workflows is easy. This book provides both step-by-step recipes and relevant background information. It covers all the important aspects of Windows Workflow Foundation 4.0. The best thing about this book is that all recipes are based on real-world experiences of Andrew Zhu. Andrew is a global WF and BizTalk technology support engineer for Microsoft. This book covers everything you need to know, when working with workflows. Get to grips with flow control activities, messaging, and transaction processes with easy to understand steps followed by explanations. You will quickly learn to use collection and custom WF activities and WF services.You will see recipes that illustrate integration of Windows Workflow with other applications such as WPF, ASP.NET, WCF service.Lastly, you will discover how easily you can customize W4 Designer with WF rule engine and others.
Table of Contents (15 chapters)
Microsoft Windows Workflow Foundation 4.0 Cookbook
Credits
About the Author
About the Reviewers
Foreword
Preface
Index

Customizing a MyReadLine activity with Bookmark


By using InArgument, OutArgument, and InOutArgument, 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.

How to do it...

  1. Create a workflow project:

    Create a new Workflow Console Application under the Chapter01 solution and name the project as UseBookmark. Next, add a code file to this project and name the file as MyReadLineActivity. We can see this in the following screenshot:

  2. Customize the activity with Bookmark:

    Fill the opening MyReadLineActivity.cs file with the following code:

    using System.Activities;
    namespace UseBookmark{
        public class MyReadLine : NativeActivity<string>{
            [RequiredArgument]
            public InArgument<string> BookmarkName { get; set; }
            protected override void Execute(
                NativeActivityContext context)
            {
                context.CreateBookmark(BookmarkName.Get(context),
                         new BookmarkCallback(OnResumeBookmark));
            }
            protected override bool CanInduceIdle
            {
                get
                {
                    { return true;}
                }
            }
            public void OnResumeBookmark(
                NativeActivityContext context,
                Bookmark bookmark,
                object obj)
            {
                Result.Set(context, (string)obj);
            }
        }
    }
    

    Save the file and press F6 to build the project so that the activity will appear in the WF designer activity toolbox.

  3. Author a workflow:

    Open Workflow1.xaml and author the workflow as shown in the following screenshot:

  4. Write code to host the workflow:

    Open Program.csfile and change the code as follows:

    using System;
    using System.Linq;
    using System.Activities;
    using System.Activities.Statements;
    using System.Threading;
    
    namespace UseBookmark{
        class Program{
            static void Main(string[] args)
            {
                AutoResetEvent syncEvent = 
                    new AutoResetEvent(false);
                string bookmarkName="GreetingBookmark";
                WorkflowApplication wfApp = 
                    new WorkflowApplication(new Workflow1()
                {
                    BookmarkNameInArg=bookmarkName
                });
                wfApp.Completed = delegate(
                    WorkflowApplicationCompletedEventArgs e)
                {
                    syncEvent.Set();
                };
                wfApp.Run();
                wfApp.ResumeBookmark(bookmarkName, 
                    Console.ReadLine());
                syncEvent.WaitOne();
            }
        }
    }
    
  5. Run it:

    Set UseBookmark as Startup project. Press Ctrl+F5 to build and run the workflow without debugging. The application should run in a console window and print the message as shown in the following screenshot:

How it works...

In the code shown in the second step, we create a class inherited from NativeActivity. NativeActivity is a special abstract activity that can be used to customize complex activities; we will talk about it more in Chapter 5, Custom Activities.

context.CreateBookmark(BookmarkName.Get(context),
        new BookmarkCallback(OnResumeBookmark));

By this statement, the WF context creates a Bookmark with arguments BookMarkName and BookMarkCallback. When the wfApp.ResumeBookmark method is called, the OnResumeBookmark that was defined in the Customized Activity body will be executed.

protected override bool CanInduceIdle{
    get
    {
        { return true;}
    }
}

This is a built-in property that indicates whether the customized activity can cause the workflow to become idle; the default value is false.

Consider the following code snippet of step 3:

wfApp.ResumeBookmark(bookmarkName, 
                Console.ReadLine());

When this statement is executed, the OnResumeBookmark method defined in the MyReadLine activity will be called and the method will accept the value passed via Console.ReadLine().