RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Using Windows Workflow to Control ASP.NET Navigation : Page 3

Windows Workflow is not only ideal for defining page-flow logic visually, but also for letting different systems communicate.

Setting Up ASP.NET
So far, you have the service layer that allows communication between WF and external items and a simple workflow that calls an external method and waits for an external event. Now you can set up an ASP.NET project.

Figure 7. Adding an ASP.NET Web Application: Use the Add New Project dialog to add an ASP.NET Web Application to your solution.
Add a new ASP.NET Web Application Project to the solution and name it MyWeb (see Figure 7).

Add System.Workflow.Activities, System.Workflow.Runtime, System.Workflow.ComponentModel, and MyWorkflows (the Sequential Workflow Console Application you created earlier) to the new project's dependencies.

Add a new class to the Web Application project named Wrapper (see Listing 2). This isn't strictly necessary to get things working, but it saves a lot of typing!

Add three Web Forms, named NameAndUserChoices.aspx, Page2.aspx, and Page3.aspx. In NameAndUserChoices add a label with the text "Name," a text box, a check-box with the caption "Go to page 2," and a button with the caption "Next." Double click the button to add a Click event handler and add the following code:

   protected void Button1_Click( object sender, EventArgs e )
      Dictionary<string, object> propertyBag = new 
         Dictionary<string, object>();
      propertyBag[@"name"] = TextBox1.Text ;
      propertyBag[@"viewPage2"] = CheckBox1.Checked ;
         new Guid( Request.QueryString[ @"id" ] ),
         Request.QueryString[ @"informationName" ],
         propertyBag );
Add some identifiable text and a Next button in Page2.aspx. Add the following code to the Next button's Click handler:

   protected void Button1_Click( object sender, EventArgs e )
         new Guid( Request.QueryString[ @"id" ] ),
         Request.QueryString[ @"informationName" ], null );
Add some identifiable text to Page3.aspx. The ASP.NET application needs an initial page on which to start the workflow. Because the initial page is determined by the workflow itself you don't actually need a physical page; instead, you can just create an HTTP handler that responds to requests for a specific page name. Add the following code to web.config under the element:

      <add path="WorkflowController.aspx" 
        type="MyWeb.WorkflowController" verb="*"/>
Add a new class named WorkflowController containing the code in Listing 3:

Ensure the following using statements are present:

   using System;
   using System.Web;
   using System.Workflow.Activities;
   using System.Workflow.Runtime;
   using MiddleBit;
The code in Listing 3 handles any request for WorkflowController.aspx. ASP.NET will call ProcessRequest, which then:

  • Creates an instance of the workflow (remember, this "page" is only called once—at the start of the workflow).
  • Gets an instance of the GenericInformationService (which you add to the ExternalDataExchangeService via the web.config file—you'll see how below).
  • Adds an event handler for the InformationRequested event.
  • Starts an instance of the workflow called Workflow1.
  • Tells the scheduler service to run the workflow identified by the GUID.
The informationRequested event handler that appears later in Listing 3 simply redirects to the appropriate page depending on what information is being requested. Note that the InstanceId for this workflow is also passed to the page.

You need to add a bit more to the web.config file. Add the following lines immediately under the start of the block in web.config:

     <section name="WorkflowRuntime" 
       System.Workflow.Runtime, Version=3.0.00000.0,  
       Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
     <section name="ExternalDataExchangeWorkflowServices" 
       System.Workflow.Activities, Version=3.0.00000.0, 
       Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
Then, anywhere under the node, add the following lines:

       <add type="MiddleBit.GenericInformationService, 
         <add type="System.Workflow.Runtime.Hosting.
           System.Workflow.Runtime, Version=3.0.00000.0, 
           Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
The configuration code adds the GenericInformationService to the ExternalDataExchangeService, and adds the ManualWorkflowSchedulerService to the workflow's list of services.

You're nearly done. You just need to tell ASP.NET to start the workflow runtime when ASP.NET starts and to stop the workflow runtime when ASP.NET stops. To do this, add a global.asax to the Web Application project (right-click on the Project item in Solution Explorer, and select Add New Component --> Global Application Class). Accept the default name of global.asax. In the Application_Start section, add the following:

   WorkflowRuntime r = new WorkflowRuntime( @"WorkflowRuntime" );
   Application[ "WorkflowRuntime" ] = r;
   ExternalDataExchangeService s =
     new ExternalDataExchangeService(
     @"ExternalDataExchangeWorkflowServices" );
   r.AddService( s );
   r.StartRuntime( );
In the Application_End section, add:

   WorkflowRuntime r = Application[ "WorkflowRuntime" ] 
      as WorkflowRuntime;
   if (r != null) 
      r.StopRuntime( );
Finally, add the following using statements to the top of the file:

   using System.Workflow.Activities;
   using System.Workflow.Runtime;
If you now debug the MyWeb project and go to WorkflowController.aspx you'll see it start the workflow runtime, add the services, create the workflow, and run it. As soon as it runs, you'll see the informationRequested event fire, which will redirect ASP.NET to NameAndUserChoices.aspx. Then, when you fill in this page, you'll see the information passed back to the workflow. Verify this by sticking breakpoints in the relevant places.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date