Hosting WF Runtime
Windows Workflow Foundation is a technology from Microsoft to help enterprise application developers in modeling real-world workflows. Using Visual Studio 2005 with the help of Visual Studio 2005 extensions for Workflow Extensions, creating a Workflow is a cake walk. Through Visual Studio one can drag-drop activities.
Workflow Runtime
To execute a Workflow, create an instance of WorkflowRuntime like below
WorkflowRuntime workflowRuntime = new
WorkflowRuntime()
WorkflowRuntime is a class under System.Workflow.Runtime namespace in system.workflow.runtime.dll. As you would assume WorkflowRuntime abstracts out the functionality of creating, hosting and managing multiple workflows.
A Workflowruntime can host, execute and manage multiple workflows simultaneously. All this is done by executing each workflow hosted, on a different thread. Since multiple threads are getting executed, each thread can complete its execution at its own time, these threads needs to be synchronized. Even if a WorkflowRuntime is hosting and executing a single workflow, the main thread on which WorkflowRuntime is getting executed and the thread (spawned by WorkflowRuntime) running to execute the workflow needs to be synchronized.
AutoResetEvent class comes handy to synchronize threads. All a developer need to do is to create an instance of AutoResetEvent class, and call Set and WaitOne methods on the object. The WaitOne methods lets an executing (main) thread to wait for a spawned thread to complete its execution. The spawned thread calls Set method to indicate the completion of its execution.
AutoResetEvent waitHandle = new
AutoResetEvent(false);
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)
{
waitHandle.Set();
};
Now we have all the stage set to create an instance of a workflow and execute it. WorkflowRuntime has a method CreateWorkflow which takes the workflow type as an argument to create a workflow instance.
// Get the workflow type
Type type = typeof(SimpleWorkflow);
// Create and start an instance of the workflow
workflowRuntime.CreateWorkflow(type, parameters).Start();
// Wait for the workflow to complete execution
waitHandle.WaitOne();
That's it. That's all is needed to host a Workflow. Isn't it simple?J
Real World scenarios
Though hosting WorkflowRuntime with console applications is simple, it's not the only way. Enterprise systems need a better way to host and manage WorkflowRuntime. Microsoft has thought about this and has come-up with different ways of hosting WorkflowRuntime.
Hosting Workflows in ASP.NET Applications
If you want your application to have a Web interface, you can host your workflows within an ASP.NET application.
Use the WorkflowRuntime object to access the workflow runtime. Applications require one instance of the workflow runtime for all the users of the application. It is possible to create this in the Application_Start event in the Global.asax file.
Use the Web.config file to configure the runtime.
If you use the WorkflowWebRequestContext object to access the workflow runtime, you cannot add services such as persistence or scheduling services because the workflow is already started. If you try to add services to the runtime when it has already started it will generate an error. Instead, to configure the runtime with services, use the <workflowRuntime> section in the Web.config file:
[Web.config]
<configuration>
<configSections>
<section name="WorkflowRuntime"
type="WorkflowRuntimeSection, System.Workflow.Runtime" />
</configSections>
</configuration>
Comments
- Anonymous
May 19, 2009
I just tried to roll my first application that implemented windows workflow foundation and ran into a bit of an issue. I am trying to call the CreateWorkflow function to get an instance of my workflow with a service running under an account without admin rights. I am getting an error message of : System.UnauthorizedAccessException: Access to the registry key 'Global' is denied. Have you or anyone else out there seen this? Do you know what registry key it needs access to?