Task 2: Configure Runtime Services using App.ConfigĀ 

Download sample

In the previous exercise, 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 needed.

NoteNote

If you completed Task 1: Configure Runtime Services Using Code, you will have to undo all the changes you made to the project because you will be 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.

NoteNote

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.

Creating and Defining the Application Configuration File

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".

    <None Include="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" />
    
    NoteNote

    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.

    NoteNote

    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 will appear similar to the following:

    <?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>
    

Using the Application Configuration File

To use the application configuration file

  • 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.

    Dim workflowRuntime As WorkflowRuntime = _
        New WorkflowRuntime("HostingWorkflowRuntime")
    
    WorkflowRuntime workflowRuntime = 
        new WorkflowRuntime("HostingWorkflowRuntime");
    

Compiling the Code

  1. Click Start, point to Programs, point to Microsoft .NET Framework SDK v2.0, and then click SDK Command Prompt.

  2. Go to the source directory of the tutorial.

  3. At the command prompt, type MSBUILD to build the project.

In Task 3: Use the Windows Workflow Persistence Service, you will 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

Footer image

Send comments about this topic to Microsoft.