Sequential Workflow Service Sample

Download sample

This sample demonstrates how to create a workflow service using a sequential workflow. This sample shows how to create a service contract in place, a method of service authoring that is referred to as the Workflow First mode. The sample creates an ICalculator contract and is implemented in the workflow as the workflow is being built.

Note

This sample requires that .NET Framework version 3.5 is installed to build and run. Visual Studio 2008 is required to open the project and solution files.

Implementing the calculator as a workflow includes the following benefits:

  • You can start a long-running process that automatically saves the workflow state. This means that when you restart the workflow service, the client can communicate with the same instance of the service.

  • The application-level protocol in the contract is enforced. If an operation is unexpectedly called on this service, the workflow raises an exception.

Sequential Workflow Services Sample

In this workflow, the PowerOn Receive activity has a property named CanCreateInstance, which is marked true. This means that an instance of this service is created when the client calls the PowerOn operation. The client receives the context as part of the reply for this operation. From that point onward, any of the other operations in the workflow can be called. For each operation the client calls, the context is sent on the channel, and therefore the operation can be routed to the correct instance. When the PowerOff operation is called, a Boolean value is set that causes the workflow to exit the while loop.

The following sample code shows the WorkflowServiceHost invocation pattern that is implemented in the Program.cs file in the service project.

WorkflowServiceHost workflowHost = new WorkflowServiceHost(typeof(Microsoft.WorkflowServices.Samples.SequentialCalculatorService));
workflowHost.Open();

In the previous sample code, the constructor for WorkflowServiceHost takes the workflow type as input and opens it to listen for messages.

This sample also contains a client application that invokes operations on the calculator service. The client application is a sequential workflow and makes a series of operation invocations. The client workflow uses Send activities to invoke operations on the workflow. The client project contains references to the ICalculator service that the Send activity can use to display the interface, from which you can select appropriate operations to invoke.

To set up, build, and run the project

  1. Perform the setup instructions listed in One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. To install the persistence providers, run the CreateStores.cmd script located in the One-Time Set Up Procedure for the Windows Communication Foundation Samples topic.

  3. This sample uses the NetFx35Samples_ServiceWorkflowStore database. It does not include a persistence store for the client workflow. This sample assumes that you are using SQL Server Express, where it installs the databases. If you prefer to install the databases in SQL Server, change the connection strings in the App.config file.

  4. If you do not want to use persistence providers, then comment out the <WorkflowRuntime> section from the App.config files.

  5. Run the sample as a user from the Administrators group. If you are using Windows Vista, right-click the client executable file and then click Run as administrator.

  6. Once the application starts running, the client workflow sends a set of calculator operations and then completes the workflow. You can press ENTER to send another set of calculator operations.

  7. To test the long-running nature of this service, put a breakpoint in the client application. When you reach the breakpoint in Visual Studio, close and then restart the service application. Release the breakpoint on the client. The client workflow talks to the same workflow instance on the service. You may also want to break the sample into two solutions to test the recycling of the service.

To run the sample on separate computers

  1. Edit the configuration files for the service and client, ensuring that you change the server name in the endpoint address. Change the server name from localhost to the machine name on which you intend to run the service.

  2. The service uses port 8888, therefore you must open this port in the firewall. From the Windows Control Panel, click Windows Firewall. Click Add Port and then add port 8888. Alternatively, you can run the service on the default port. To do so, remove :8888 from the endpoint address.

  3. Edit the client configuration file and add the following element as a child of the <endpoint> element.

    <identity>
        <UserPrincipalName value=”*@<Domain Name in which your server is running” />
    </identity>
    

    To determine the domain name, start the service on the computer on which you intend to run it. In a command prompt window on the other computer, run the following command.

    svcutil.exe http://<serverName>:8888/servicehost/Calculator.svc

    This command generates a .cs file and an output.config file. In the <endpoint> element of the configuration file, copy the <identity> element to your client configuration file.

© 2007 Microsoft Corporation. All rights reserved.