Condividi tramite


Task 2: Configure Runtime Services using App.Config

Download sample

In Task 1: Configure Runtime Services Using Code, you added the DefaultWorkflowSchedulerService service to the Windows Workflow Foundation runtime engine by calling the AddService method that is defined in the WorkflowRuntime class. In this exercise, you use a different method to configure runtime services: you use an application configuration file.

Using a configuration file is a more flexible alternative because it enables you to create several different configurations of the Windows Workflow runtime engine that your application can use. For example, you can create a configuration that does not use any services or you can create a runtime engine configuration that uses persistence and tracking during workflow execution. Additionally, these different configurations can be defined in the same configuration file and accessed by your host application as required.

Note

If you completed Task 1: Configure Runtime Services Using Code, you must undo all of the changes you made to the project because you are using a configuration file to configure runtime services. The project files provided with this task do not contain the code added in the previous task and therefore provides a good starting point.

Note

While you are encouraged to follow the exercises in a linear manner, it is not required. You can start this exercise by opening the sample project and proceeding to the steps in the following section.

To create the application configuration file

  1. Create a new file named App.config in your project directory.

  2. In the HostingWorkflows project file, in the ItemGroup element that contains your source files, add a new element named None.

  3. Add an attribute named Include with the value "App.config".

To define the application configuration file

  1. In the App.config file, create the standard XML-processing instruction with UTF-8 encoding.

    <?xml version="1.0" encoding="utf-8" ?>
    
  2. Create a root element named configuration.

    <configuration>
    </configuration>
    
  3. In the configuration element, create a new element named configSections.

    <configSections>
    </configSections>
    
  4. In the configSections element, create a new element named section.

  5. Add an attribute named name with the value "HostingWorkflowRuntime".

  6. Add another attribute named type with the value "System.Workflow.Runtime.Configuration.WorkflowRuntimeSection, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35".

    <section name="HostingWorkflowRuntime" type="System.Workflow.Runtime.Configuration.WorkflowRuntimeSection, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    

    Note

    You can create as many configuration sections as you want. For this exercise, however, you only have to create one.

  7. Create a new element in the configuration node named HostingWorkflowRuntime that contains an attribute named name with the value "Hosting".

    Note

    This element name is the same name that you specified in the name attribute for the section that you created in step 5. If you create multiple configuration sections, create new elements that correspond to each section name.

    <HostingWorkflowRuntime Name="Hosting">
    </HostingWorkflowRuntime>
    
  8. In the HostingWorkflowRuntime element, create a new element named CommonParameters.

    <CommonParameters/>
    
  9. In the HostingWorkflowRuntime element, create a new element named Services.

    <Services>
    </Services>
    
  10. In the Services element, create a new element named add.

  11. Add an attribute named type with the value System.Workflow.Runtime.Hosting.DefaultWorkflowSchedulerService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.

  12. Add another attribute named maxSimultaneousWorkflows with the value 1.

    <add type="System.Workflow.Runtime.Hosting.DefaultWorkflowSchedulerService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  maxSimultaneousWorkflows="1"/>
    
  13. Your App.config file appears similar to the following configuration.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <section name="HostingWorkflowRuntime" type="System.Workflow.Runtime.Configuration.WorkflowRuntimeSection, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </configSections>
        <HostingWorkflowRuntime Name="Hosting">
            <CommonParameters/>
            <Services>
                <add type="System.Workflow.Runtime.Hosting.DefaultWorkflowSchedulerService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  maxSimultaneousWorkflows="1"/>
            </Services>
        </HostingWorkflowRuntime>
    </configuration>
    

To use the application configuration file

  1. In the Main method of the Program class, add a parameter to the WorkflowRuntime constructor that is used to instantiate the WorkflowRuntime object.

    This parameter is a String type that specifies the name of the configuration section to use.

Compiling the Code

For information about compiling your code, see Compiling the Code.

In Task 3: Use the Windows Workflow Persistence Service, you learn how to use the SqlWorkflowPersistenceService service to store the current state of your workflow.

See Also

Reference

DefaultWorkflowSchedulerService
System.Workflow.Runtime.Configuration

Concepts

How to: Add and Remove Workflow Services

Other Resources

Task 3: Use the Windows Workflow Persistence Service
Workflow Configuration Formats

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04