ASP.NET and Workflows
Last week i got my first hands on experience of working on an exciting new technology called Windows Workflow Foundation which ships with the WinFX package. The best part is you have WWF add-ins for VS2005 for designing your workflow. For someone who has no prior experience of using Biztalk, the concept of visually designed workflows is hard to sink in. But when you start of on a new technology you are bound to face some initial hurdles. I would like to highlight one of the major ones here.
I created a workflow runtime object using the new operator and the WorkflowRuntime class constructor, like so:
WorkflowRuntime wr = new WorkflowRuntime();
Using this code in ASP.NET is fine as long as you place it in Application_Start and then find a way to make the object that is created reachable from within your page code. Instantiating the workflow runtime for each request results in an exception because the workflow runtime cannot be loaded more than once in the same AppDomain. Two ASP.NET requests have distinct state and context, but run in the same AppDomain. As a result, the first request works while the second throws an exception.
Windows Workflow Foundation provides a built-in infrastructure to guarantee that there's exactly one workflow runtime object per AppDomain and that you don't have to worry about its creation and instantiation. In ASP.NET, in fact, you can use the WorkflowWebRequestContext class to access the current and correct instance of the workflow runtime. This code shows how to get a reference to the workflow runtime:
WorkflowRuntime wr =
Internally, the get accessor of the Current property calls into a public member of a static class that caches the workflow runtime instance, creating a new instance if one cannot be found. Once you've got a reference to the runtime object, you can register the handler for the WorkflowCompleted event and start the actual workflow. This code shows how:
Type t = typeof(Samples.LoginWorkflow);
WorkflowInstance instance = wr.CreateWorkflow(t, parameters);