Sdílet prostřednictvím


Task 3: Create a Durable Service Client

Download sample

In this task, you will create a simple client to invoke operations on your durable service. The testing tool WcfTestClient.exe is used by the WCF Service Library template.

Creating the client

  1. In the SimpleDurableService solution, right-click the solution node in the Solution Explorer pane.

  2. Navigate to Add and select New Project.

  3. In the New Project dialog box, under Windows, select Console Application.

  4. Name your project DurableServiceTestClient and click OK.

  5. Right-click the DurableServiceTestClient project node and select Add Reference.

  6. In the Add Reference dialog box, in the .NET tab, select the System.ServiceModel assembly and click OK.

  7. Add the following using statement to your C# source file:

Imports System.ServiceModel

If you created a Visual Basic solution, right-click the DurableServiceTestClient project node and select Properties. Select the References tab, and under Imported Namespaces, check System.ServiceModel.

  1. Use SvcUtil.exe to generate the proxy code and configuration file for your durable service.

    Using SvcUtil.exe

    To use SvcUtil.exe, see ServiceModel Metadata Utility Tool.

    After you have generated your proxy code and configuration files, add those files to your DurableServiceTestClient project by doing the following:

    1. Navigate to the Solution Explorer pane.

    2. Right-click the DurableServiceTestClient project node.

    3. Highlight Add and select Existing Item.

    4. Navigate to the folder where SvcUtil.exe generated the configuration and proxy code files.

    5. Select the files and click OK.

    6. Rename your configuration file to App.config if it is not named that already.

  2. Add the following code to your Main method implementation:

Sub Main()
    Dim client As New Service1Client("WSHttpContextBinding_IService1")
    client.Open()
    Dim currentValue As Integer = Nothing
    currentValue = client.Add(3)
    Console.WriteLine("The current value is {0}", currentValue)
    currentValue = client.Subtract(4)
    Console.WriteLine("The current value is {0}", currentValue)
    currentValue = client.Multiply(5)
    Console.WriteLine("The current value is {0}", currentValue)
    client.EndPersistence()
    Console.WriteLine("Press <ENTER> to shut down the client.")
    Console.ReadLine()
    client.Close()
End Sub
static void Main(string[] args)
{
    Service1Client client = new Service1Client("WSHttpContextBinding_IService1");
    client.Open();
    int currentValue = default(int);
    currentValue = client.Add(3);
    Console.WriteLine("The current value is {0}", currentValue);
    currentValue = client.Subtract(4);
    Console.WriteLine("The current value is {0}", currentValue);
    currentValue = client.Multiply(5);
    Console.WriteLine("The current value is {0}", currentValue);
    client.EndPersistence();
    Console.WriteLine("Press <ENTER> to shut down the client.");
    Console.ReadLine();
    client.Close();
}

Durable service clients are created in the same way as regular WCF service clients. A new instance of the client is created based off of the proxy code of the service. Next, a call to Open is made followed by the invocation of the service operations. Finally, a call to Close is made to shut down the client.

  1. To use the client with your durable service in the SimpleDurableService solution, you must delete a command-line argument from the SimpleDurableService project properties. To do so, perform the following steps:

    1. Right-click the SimpleDurableService project node and select Properties.

    2. Select the Debug tab, and in the detail pane under Start Options, remove /client:"WfcTestClient.exe" from the text box.

  2. Right-click the SimpleDurableService solution node and select Properties.

  3. In the Property Pages dialog box, select Multiple startup projects.

  4. If SimpleDurableService is not listed as the top item in the list, use the arrows on the side of the list box to make it the top item. This is needed so that your service starts running before your client application tries to invoke any operations on the service.

  5. For each project in the list, change the action from None to Start.

  6. Click Apply and then OK.

  7. Build both the service and client projects. You can host your durable service in IIS, a Windows application, or any other host process, but for the purposes of this tutorial, you will host it in WfcSvcHost.exe. WcfSvcHost.exe is a developer tool that hosts services for testing purposes.

    You should observe the following output from the client's command-line interface:

    The current value is 3
    The current value is -1
    The current value is -5
    Press <ENTER> to shut down the client.
    

    One way to observe that persistence is occurring after every operation (with the exception of EndPersistence), is to install Microsoft SQL Server Management Studio Express from https://www.microsoft.com/downloads and set debug points throughout your client app. After you hit those points, observe the InstanceData table in the NetFx35Samples_DurableServiceStore database. You should see a new entry being added to the table, then updated after every operation until the EndPersistence call completes, at which time the table entry will be removed.

    Another way to see the value of the durable service is to make the first operation invocations to the service from your client and in Visual Studio put a breakpoint on the operation invocation. Before executing the third operation invocation, shut the service down and restart the service. Now make the operation invocation. The state of the service is now restored from the persistence database and you will be able to see that the service gives you back the reply desired for the current value.

See Also

Tasks

Task 1: Define and Implement the Durable Service Contract

Other Resources

Task 2: Enable Persistence for the Durable Service
Tutorial: Create a Durable Service

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