Thursday, March 17, 2011

ASP.NET WF4 / WCF and Async Calls

How should you use WF4 and WCF with ASP.NET
For this post I’ve created a really simple workflow and WCF service that delay for a specific amount of time and then return a value.  Then I created an ASP.NET page that I can use to invoke the workflow and WCF service to test their behavior

The Workflow Definition

First off – let’s get one thing straight.  When you create a workflow, you are creating a workflow definition.  The workflow definition still has to be validated, expressions compiled etc. and this work only needs to be done once. When you hand this workflow definition to WorkflowInvoker or WorkflowApplication it will create a new instance of the workflow (which you will never see).

To make this clear, in this sample I have a workflow file SayHello.xaml but I named the variable SayHelloDefinition.

private static readonly Activity SayHelloDefinition = new SayHello();

The Easy Way

If you want to do something really simple, you can just invoke workflows and services synchronously using WorkflowInvoker

private void InvokeWorkflow(int delay)
{
var input = new Dictionary<string, object> { { "Name", this.TextBoxName.Text }, { "Delay", delay } };
this.Trace.Write(string.Format("Starting workflow on thread {0}", Thread.CurrentThread.ManagedThreadId));
var output = WorkflowInvoker.Invoke(SayHelloDefinition, input);
this.Trace.Write(string.Format("Completed workflow on thread {0}", Thread.CurrentThread.ManagedThreadId));
this.LabelGreeting.Text = output["Greeting"].ToString();
}

Read more: Ron Jacobs