Friday, November 30, 2007

Playing around with Workflow Foundation in F#

I am also investigating Workflow Foundation. While workflow foundation is made a lot easier with appropriate GUI tool support, I still find it instructive to implement some examples in F# and see how it works. Here's a very simplistic workflow example impleted in F#:

#light
#I @"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0"
#r @"System.Workflow.Activities.dll"
#r @"System.Workflow.ComponentModel.dll"
#r @"System.Workflow.Runtime.dll"

open System
open System.Collections
open System.Threading
open System.Workflow.Activities
open System.Workflow.ComponentModel
open System.Workflow.Runtime
//
// Build a simple workflow
//
type Workflow1 = class
inherit SequentialWorkflowActivity as base

val mutable createMessage : CodeActivity
val mutable firstName : string
val mutable message : string

new() as this = {createMessage = null;firstName=null;message=null} then
this.InitializeComponent()

member this.FirstName with set value = this.firstName <- value

member this.Message with get() = this.message

(* Define workflow task *)
member this.composeMessage sender (e:EventArgs) =
this.message <- "Hello " + this.firstName + "!"

(* Normally, Visual Studio would generate this with the help of GUI tools *)
member this.InitializeComponent () =
this.CanModifyActivities <- true
this.createMessage <- new System.Workflow.Activities.CodeActivity()

// create Message
this.createMessage.Name <- "createMessage"
this.createMessage.ExecuteCode.AddHandler
(new System.EventHandler(this.composeMessage))


// Workflow1
this.Activities.Add(this.createMessage)
this.Name <- "Workflow1"
this.CanModifyActivities <- false

end
//
// Workflow Engine Hosting Code
//
let main =

using (new WorkflowRuntime()) (fun wfRuntime ->
let waitHandle = new AutoResetEvent(false)

wfRuntime.WorkflowCompleted.Add
(fun (e:WorkflowCompletedEventArgs) ->
let message = e.OutputParameters.["Message"]
Console.WriteLine(message)
waitHandle.Set() |> ignore)

wfRuntime.WorkflowTerminated.Add
(fun (e:WorkflowTerminatedEventArgs) ->
Console.WriteLine(e.Exception.Message)
waitHandle.Set()|>ignore)

let inparam = new Generic.Dictionary<string, obj>()
inparam.Add("FirstName",box("Joe"))

let instance = wfRuntime.CreateWorkflow(typeof<Workflow1>,inparam)
instance.Start() |> ignore
waitHandle.WaitOne() |> ignore
Console.WriteLine("Done!")
)

// Execute the workflow
do main

1 comment:

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!