Calculator Client Sample

Download sample

The calculator client application is an application based on Windows Presentation Foundation (WPF) that you can use to send operations to the calculator state machine service or the durable calculator service. You can use the calculator service with different protocols, such as the WSHttpContextBinding or WsHttpBinding bindings with cookies enabled. You can switch between the bindings and still communicate with the same workflow service instance.


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.

You can also turn on the calculator, perform operations, and close the calculator application without turning it off. You can then turn on the calculator, and the calculator communicates with the workflow instance that it was communicating with before. The context is stored in a file called Context.ctx, which is stored in the \bin directory of the calculator client application. To restart the calculator client application, you can delete the Client.ctx file.

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

The following code shows the ICalculator service contract.

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

To set up the calculator

  1. Ensure that the State Machine Workflow Service Sample or the Durable Services is hosted in Internet Information Services (IIS) 6.0 according to the instructions in the respective samples. The service you install has its Service.svc and Web.config files in the %systemdrive%\intetpub\wwwroot\ServiceModelSamples directory. The Service.dll file is in the %systemdrive%\intetpub\wwwroot\ServiceModelSamples\bin directory.

  2. Download the Workflow Service Utilities and save it so that the CalculatorClient and WorkflowServiceUtility folders are located in the same parent folder.

  3. Make sure to properly create the workflow service reference. If it is undefined when you open the solution, re-create the reference. You can also look at the Web Services Descriptor Language (WSDL) of the state machine workflow service or the durable calculator service by pointing your browser at the appropriate address, which is http://localhost/ServiceModelSamples/service.svc. To look at the WSDL, point to http://localhost/ServiceModelSamples/service.svc?wsdl.

  4. The calculator client demonstrates how to communicate with a service while preserving state, so that the service can continue when you restart the conversation. To use the calculator, input a number and an operation that you want to perform. The service with which the calculator client communicates performs the specified operation using the result of the last operation. When you start, the result of the last operation is 0. For example, if you start the calculator client and input the number 2, then input the addition operation (+), the service adds 2 to the result of the last operation (in this case 0). The calculator then displays the result (2). If you then input 3, then the addition operation (+), the number 3 is added to the result of the last operation (2), and the result is 5. The equals sign (=) adds 0 to the result of the last operation. In the preceding example, if you input the number 2 and then the equals sign, the calculator adds 0 to the result of the last operation, which was 5, and returns 5.

  5. If you terminate and then restart the client, the result of the last operation is shown on the calculator. You can continue to perform operations from that point forward.

  6. The calculator client uses the Client.ctx text file to store the context in a durable location the first time a call is made (in this case, in the \bin directory of your sample). When you reopen the client, it checks whether the file is present. If it is, the client applies the stored context to the channel that is being created. If the workflow service has finished, and you open the client with the Context.ctx file still in your \bin directory, the calculator client attempts to apply the context to the channel. You receive an error because the workflow instance with which you want to communicate is absent. Delete the file and try again.

© 2007 Microsoft Corporation. All rights reserved.