Workflow Application Sample

Microsoft .NET Framework 3.5 Documentation

Overview

This sample demonstrates a basic Windows Workflow Foundation (WF) application, showcasing how to define, host, and execute a simple workflow.

Sample ID: WF-Workflow-App-001
Platform: .NET Framework 3.5
Technology: Windows Workflow Foundation (WF)

Features

Workflow Definition (BasicWorkflow.xaml)

The workflow is defined in a XAML file. This example uses a simple SequentialWorkflow with an WriteLine activity.

<Activity mc:Ignorable="sap"
                   xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
                   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                   xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
                   x:Class="BasicWorkflow"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <x:Members>
        <x:Property Name="Message" Type="x:String" />
    </x:Members>
    <WriteLine Text="{x:Bind Message}" />
</Activity>

Host Application (Program.cs)

The C# host application loads the workflow from the XAML file and executes it.

using System;
using System.Activities;
using System.IO;

public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Loading workflow...");

        // Load the workflow definition from XAML
        string workflowXaml = File.ReadAllText("BasicWorkflow.xaml");
        Activity workflow = ActivityXamlServices.Load(new StringReader(workflowXaml));

        // Define workflow input parameters
        var inputs = new Dictionary<string, object>
        {
            { "Message", "Hello from the Workflow Application Sample!" }
        };

        Console.WriteLine("Executing workflow...");

        // Create a workflow host and run the workflow
        var host = new WorkflowInvoker(workflow);
        var outputs = host.Invoke(inputs);

        Console.WriteLine("Workflow executed successfully.");

        // Access any workflow outputs (if any)
        // foreach (var output in outputs)
        // {
        //     Console.WriteLine($"Output: {output.Key} = {output.Value}");
        // }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

How to Run

  1. Ensure you have the .NET Framework 3.5 SDK installed.
  2. Create a new C# Console Application project in Visual Studio.
  3. Add a new XAML file named BasicWorkflow.xaml and paste the XAML code above.
  4. Replace the content of your Program.cs file with the C# host application code.
  5. Add a reference to System.Activities.dll.
  6. Build and run the application.

Download Sample

Download the complete source code for this sample:

Download Sample (ZIP)