Create and start an ASP. NET Workflow

Source: Internet
Author: User

The Windows Workflow Foundation is created to run in different host application environments. ASP. NET Web forms are such a supporting environment. However, when creating a Web-based Host application for Windows Workflow Foundation, you must design the host infrastructure to solve ASP.. NET Applications and traditional Windows Forms applications. For example, an ASP. NET application can provide services for multiple synchronous users at the same time. In this server environment, the application must be designed to effectively use the available system memory. In these cases, the Windows Workflow Foundation provides the SqlWorkflowPersistenceService service to uninstall the Workflow instance. In addition, ASP. NET will send a response when receiving the request. By default, a workflow is executed asynchronously during running. Therefore, the page may be displayed and the response may be sent before the workflow is complete. To avoid this, Windows Workflow Foundation provides the ManualWorkflowSchedulerService service to run workflows synchronously. This allows your Web form to return the workflow status information to the user.

Create an ASP. NET workflow: Create a WorkflowRuntime object.

The Global. asax file in ASP. NET can be used to process Web form events related to various Web sessions or events that are triggered when a Web application is started or ended. Session objects in ASP. NET are created for users who request Web pages. Application objects are a single object shared in each Session. The following example demonstrates how to handle the Application_Start event to create a WorkflowRuntime instance and add ManualWorkflowSchedulerService. After this operation is completed, you can save the WorkflowRuntime instance to the Application object provided with ASP. NET when you start the running by using the StartRuntime method. In future requests to Web pages in the application, you can retrieve this single WorkflowRuntime instance to start the workflow.

 
 
  1. void Application_Start(object sender, EventArgs e)   
  2. {  
  3.     System.Workflow.Runtime.WorkflowRuntime workflowRuntime =  
  4.         new System.Workflow.Runtime.WorkflowRuntime();  
  5.  
  6.     System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService manualService =  
  7.         new System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService();  
  8.     workflowRuntime.AddService(manualService);  
  9.       
  10.     workflowRuntime.StartRuntime();  
  11.  
  12.     Application["WorkflowRuntime"] = workflowRuntime;             
  13. }  
  14.  

When the application is completed, ASP. NET triggers the Application_End event. The following code retrieves the WorkflowRuntime object created during the Application_Start event to call the StopRuntime method.

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

Start an ASP. NET workflow: Start a workflow in an ASP. NET Web form.

In the previous section, a WorkflowRuntime instance is created during the Application_Start event. This object is retained until the Web application processes the request. The following code demonstrates how to retrieve a WorkflowRuntime instance from an Application object in ASP. NET. Then, use the GetService method to retrieve the ManualWorkflowSchedulerService used to start the workflow to run the workflow synchronously. Therefore, call the CreateWorkflow method defined in the WorkflowRuntime class, and then call the Start method in the WorkflowInstance object returned from the CreateWorkflow call. Since ManualWorkflowSchedulerService is added to WorkflowRuntime, The RunWorkflow method is called to pass the InstanceId of the WorkflowInstance object.

 
 
  1. protected void StartRuntime_Click(object sender, EventArgs e)  
  2. {  
  3.     WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime;  
  4.     ManualWorkflowSchedulerService manualScheduler =   
  5.         workflowRuntime.GetService(typeof(ManualWorkflowSchedulerService))   
  6.         as ManualWorkflowSchedulerService;  
  7.  
  8.     WorkflowInstance instance = workflowRuntime.CreateWorkflow(  
  9.         typeof(ASPNetSequentialWorkflow));  
  10.     instance.Start();  
  11.     manualScheduler.RunWorkflow(instance.InstanceId);  
  12. }  

The above describes how to create and start an ASP. NET workflow.

  1. ASP. NET database connection class ClassConn and oledb database connection method
  2. Implementation of ASP. NET database Driver Class: DBHelper
  3. ASP. NET database operation class written in C #
  4. ASP. NET database image storage to Sql2000
  5. Add an ASP. NET custom error handling page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.