Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

Windows Workflow Foundation 4.0: Hello, workflow!

With Visual Studio 2010 we can also try out Windows Workflow Foundation 4.0. This posting introduces simple workflow with one new activity and provides you with some tips how to get Training Kit example running. Also I prepared some screenshots for you to show how new workflow designer looks like. I have also a little gift for you – Visual Studio 2010 solution with example shown here.

WriteLine activity

When creating new sequential workflow the new project contains workflow Sequence1 by default. We will use this workflow in this example. When new project with workflow is created open workflow and add new WriteLine activity to workflow.

WriteLine activity is new activity that prints given line to output stream.

You can see new look of workflow designer when you look at image on right. I like current design more but I think I’m also happy with new one.

Now let’s assign some text to WriteLine activity. Just select the activity and press F4 to open properties window. There are two properties – Text and TextWriter. We assign value only to Text. Take a look at the following screenshot.

Now here is the tricky part. By its type Text property is expression. In our case this expression is string constant and we must quote the string. Otherwise we get workflow compilation errors at runtime.

Source files

Now let’s take a look at source files. Although we have only few of them, they may be interesting to programmers who have used workflows before. As a first thing let’s look at Sequence1.xaml. This file defines our only activity.


<p:Activity mc:Ignorable="" x:Class="Workflow1.Sequence1" 
xmlns
="http://schemas.microsoft.com/netfx/2009/xaml/activities/design"
xmlns:__Sequence1="clr-namespace:Workflow1;"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities"
xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <p:Sequence 
sad:XamlDebuggerXmlReader.FileName="C:\Path To\Sequence1.xaml">
    <p:WriteLine>["Hello,workflow"]</p:WriteLine>
  </p:Sequence>
</p:Activity>

This is the code generated by Visual Studio. I added here last two lines that are made bold. Without these lines you cannot see WriteLine activity output because console window is closed immediately.


using System;
using System.Linq;
using System.Threading;
using System.Activities;
using System.Activities.Statements;
 
namespace Workflow1
{
    class Program
    {
        static void Main(string[] args)
        {
            AutoResetEvent syncEvent = new AutoResetEvent(false);
 
            WorkflowInstance myInstance = new WorkflowInstance(new Sequence1());
            myInstance.OnCompleted = delegate(WorkflowCompletedEventArgs e) { syncEvent.Set(); };
            myInstance.OnUnhandledException = delegate(WorkflowUnhandledExceptionEventArgs e)
            {
                Console.WriteLine(e.UnhandledException.ToString());
                return UnhandledExceptionAction.Terminate;
            };
            myInstance.OnAborted = delegate(WorkflowAbortedEventArgs e)
            {
                Console.WriteLine(e.Reason);
                syncEvent.Set();
            };
 
            myInstance.Run();
 
            syncEvent.WaitOne();
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }
    }
}

This example was very easy and primitive and you saw here some basic things. In some of my near future postings I will introduce more new stuff that WF 4.0 brings us.

Download example solution


kick it on DotNetKicks.com pimp it Progg it Shout it

Comments

No Comments