Partager via


Using Workflows with ASP.NET

Windows Workflow Foundation was created to run in varying host application environments. ASP.NET Web Forms is one such supported environment. However, when you are creating a Web-based hosting application for Windows Workflow Foundation, you must design your hosting infrastructure to account for key architectural differences between an ASP.NET application and a traditional Windows Form application. For example, ASP.NET applications may serve several synchronous users at one time. In a server environment such as this, your application must be designed in a way that uses the available system memory efficiently. Windows Workflow Foundation provides the SqlWorkflowPersistenceService service to unload workflow instances in these cases. Additionally, ASP.NET sends a response when it receives a request. By default, workflows are executed by the workflow runtime in an asynchronous manner. Because of this, a page may render and a response may be sent before a workflow has completed. To circumvent this, Windows Workflow Foundation provides the ManualWorkflowSchedulerService service in order to run workflows synchronously. This enables your Web Form to return workflow state information back to the user.

Creating the WorkflowRuntime Object

The Global.asax file in ASP.NET can be used to handle Web Form events related to individual Web sessions or events that are raised when your Web application begins or ends. The Session object in ASP.NET is created for each user who requests a Web page and the Application object is a single object shared throughout each session. The following example demonstrates how you can handle the Application_Start event to create a WorkflowRuntime instance and add the ManualWorkflowSchedulerService. After this is completed and the runtime is started by using the StartRuntime method, you can save the WorkflowRuntime instance into the Application object provided with ASP.NET. In future requests for Web pages in your application, you can retrieve this single WorkflowRuntime instance to start a workflow.

void Application_Start(object sender, EventArgs e) 
{
    System.Workflow.Runtime.WorkflowRuntime workflowRuntime =
        new System.Workflow.Runtime.WorkflowRuntime();

    System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService manualService =
        new System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService();
    workflowRuntime.AddService(manualService);
    
    workflowRuntime.StartRuntime();

    Application["WorkflowRuntime"] = workflowRuntime;           
}

When your application is finished, ASP.NET raises the Application_End event. The following code shows how to retrieve the WorkflowRuntime object that is created during the Application_Start event in order to call the StopRuntime method.

void Application_End(object sender, EventArgs e) 
{
    System.Workflow.Runtime.WorkflowRuntime workflowRuntime =
        Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime;
    workflowRuntime.StopRuntime();
}

Starting Workflows in ASP.NET Web Forms

In the previous section, a WorkflowRuntime instance was created during the Application_Start event. This object remains as long as the Web application is handling requests. The following code shows how to retrieve the WorkflowRuntime instance from the Application object in ASP.NET. After you do this, use the GetService method to retrieve the ManualWorkflowSchedulerService that is used to start a workflow in order to run the workflow synchronously. To do this, call the CreateWorkflow method that is defined in the WorkflowRuntime class followed by the Start method from the WorkflowInstance object returned from the CreateWorkflow call. Because the ManualWorkflowSchedulerService was added to the WorkflowRuntime, call the RunWorkflow method, passing the InstanceId of the WorkflowInstance object.

protected void StartRuntime_Click(object sender, EventArgs e)
{
    WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime;
    ManualWorkflowSchedulerService manualScheduler = 
        workflowRuntime.GetService(typeof(ManualWorkflowSchedulerService)) 
        as ManualWorkflowSchedulerService;

    WorkflowInstance instance = workflowRuntime.CreateWorkflow(
        typeof(ASPNetSequentialWorkflow));
    instance.Start();
    manualScheduler.RunWorkflow(instance.InstanceId);
}

See Also

Reference

WorkflowRuntime
ManualWorkflowSchedulerService
GetService
RunWorkflow
WorkflowInstance
StartRuntime

Concepts

How to: Add and Remove Workflow Services

Other Resources

Developing ASP.NET Workflow Applications