State Machine Workflow Service Sample

Download sample

This sample demonstrates how to create a workflow service using a state machine workflow, shows how to implement a calculator using a state machine workflow, and demonstrates a long-running process where the state is saved automatically. When you restart the workflow service, the client can communicate with the same instance of the workflow service because the state of the workflow is maintained. The sample also demonstrates how the application-level protocols in the contract can be enforced so that the workflow raises an exception if an operation is unexpectedly called on the service.

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.

For more information about setting up this sample, see One-Time Set Up Procedure for the Windows Communication Foundation Samples.

The service implements the ICalculator service contract shown in the following sample code.

[ServiceContract(Namespace = "http://Microsoft.WorkflowServices.Samples")]
public interface ICalculator
{
    [OperationContract()]
    int PowerOn();
    [OperationContract()]
    int Add(int value);
    [OperationContract()]
    int Subtract(int value);
    [OperationContract()]
    int Multiply(int value);
    [OperationContract()]
    int Divide(int value);
    [OperationContract()]
    void PowerOff();
}

The sample implements a set of states, each of which has a set of event handlers. Each event handler contains a Receive activity that implements an operation on the ICalculator contract. Because it is in a given state, the state machine workflow can accept the set of operations that the Receive activities implement in that state. This helps to enforce an application-level protocol.

The Web.config file defines the bindings for the service and exposes two endpoints with two different bindings: a BasicHttpContextBinding with cookies enabled and the WSHttpContextBinding for clients that do not want to use BasicHttpContextBinding. The WSHttpContextBinding helps maintain the context that is used to route the requests to the particular workflow instance.

To set up the service in Internet Information Services (IIS)

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

  2. In IIS, enable Windows Authentication on the ServiceModelSamples virtual directory.

    To enable Windows Authentication in IIS 5.1 or 6.0:

    1. Open a command prompt window and type start inetmgr to open the Internet Information Services (IIS) MMC snap-in.

    2. Right-click the virtual root ServiceModelSamples inside Default Web Site, click Properties, and then click the Directory Security tab.

    3. Under Authentication and Access Control, click Edit.

    4. In the Authentication Methods dialog box, select Integrated Windows Authentication.

    To enable Windows Authentication in IIS 7.0:

    1. Open a command prompt window and type start inetmgr to open the Internet Information Services (IIS) MMC snap-in.

    2. Select the ServiceModelSamples virtual root inside Default Web Site.

    3. Inside the ServiceModelSamples home pane, double-click Authentication inside the IIS group.

    4. Select Windows Authentication and select the Enable action.

  3. Build the project. The project builds and updates ServiceModelSamples.

  4. To allow access to durable store:

    1. Run the CreateStores.cmd script located in the One-Time Set Up Procedure for the Windows Communication Foundation Samples topic. This sample uses the NetFx35Samples_ServiceWorkflowStore database.

    2. Make the ASP.NET user account a member of the SQL Server user group.

  5. To ensure that the service is properly installed, point your browser to the address https://localhost/ServiceModelSamples/service.svc. You should see the help page for the service. To look at the Web Services Descriptor Language (WSDL), type https://localhost/ServiceModelSamples/service.svc?wsdl.

  6. To run this sample, you must use the Calculator Client Sample. It is a calculator user interface created using Windows Presentation Foundation (WPF) that acts as a client for the service. You can use different endpoints that correspond to the bindings the service provides. To change the bindings, click the EndPoint menu item and select the appropriate binding, either BasicHtttpContextBinding or WSHttpContextBinding.

  7. To test the durable nature of the service, close and reopen the client while the calculator client is running. The calculator client communicates back to the same service instance and displays the instance ID at the bottom. The calculator client uses a text file called Client.ctx to store the context in a durable place the first time a call is made (in this case, the \bin directory of your sample). When you reopen the client, it checks whether the file is present. If the text file is present, it applies the stored context to the channel that is being created. If the workflow service has finished and you open the client with the Client.ctx file still in your \bin directory, it attempts to apply the context to the channel. You receive an error because the workflow instance with which you want to communicate is not there. Delete the file and try again.

  8. You can also recycle the workflow service by restarting IIS. Because you are using a persistence store after every operation, the state of the service is stored. Therefore, when you try to communicate with the service from the client after IIS has restarted, the workflow infrastructure receives the workflow instance from the persistence store and you can communicate with the same instance.

    Note

    When you invoke an operation for the first time after restarting IIS, you receive a MessageSecurityException exception, which is caused by an expired security token on the channel. Invoke another operation and it will succeed.

© 2007 Microsoft Corporation. All rights reserved.