Building a WCF Client


You can use the tools provided by WCF and Visual Studio 2005 to build a simple client application to test the WCF service. In the following exercises, you will build a console client application that invokes each of the operations defined by the service, and verify that the service operates as expected.

Build a console client application

  1. In Visual Studio 2005, in Solution Explorer, right-click the ProductsService solution, point to Add, and then click New Project.

  2. In the Add New Project window, select the Windows project type, click the Console Application template, set the name of the project to ProductsClient, and save it in the default folder for the solution. Click OK.

    image from book

  3. Make sure you have selected the ProductsClient project in Solution Explorer. On the Project menu, click Add Reference, and add a reference to the System.ServiceModel assembly.

  4. On the Project menu, click Add Service Reference. In the Add Service Reference dialog box, type http://localhost/ProductsService/ProductsService.svc?wsdl for the service URL, type ProductsService for the service reference name, and then click OK.

    image from book

    This action queries the ProductsService service, retrieves the metadata, and generates a proxy class using this metadata. The client can use this proxy class to invoke the operations exposed by the ProductsService service. You can view the code for the proxy class by expanding the Service References folder in Solution Explorer, expanding the image from book ProductsService.map folder, and double-clicking the image from book ProductsService.cs file. Be careful not to change any of the code in this file.

  5. In Solution Explorer, double-click the image from book app.config file for the ProductsClient project to display this file in the code view window. This is the WCF client configuration file, and it was generated at the same time as the proxy class. It contains the settings the client uses to connect to the WCF service. Examine the <client> section towards the bottom of the file:

     <client>     <endpoint address="http://lon-dev-01/ProductsService/ProductsService.svc"               binding="basicHttpBinding" bindingConfiguration= "BasicHttpBinding_IProductsService"               contract="ProductsClient.ProductsService.IProductsService"               name="BasicHttpBinding_IProductsService" /> </client>

    Note 

    Your file will contain the name of your computer in the endpoint address, rather than lon-dev-01.

    The endpoint mirrors that of the ProductsService service, specifying the URL of the service, and the same binding and contract information. The main difference is the addition of the name attribute, enabling you to refer to the endpoint in your code (which you will do later in this exercise). Also, you can see that the file customizes the settings used by the basicHttpBinding binding. If you scroll back through the file, you can see the definition of this customized binding. Many of the predefined bindings available with WCF have a number of optional parameters that you can modify in this way. Do not change anything in this file.

  6. Display the image from book Program.cs file for the ProductsClient project in the code view window. Add the following statements to the top of the file:

     using System.ServiceModel; using ProductsClient.ProductsService;

    You should always add a reference to the System.ServiceModel assembly and namespace to a WCF client application, as they provide the methods needed to communicate with a WCF service. The ProductsClient.ProductsService namespace contains the proxy class for the ProductsService WCF service.

  7. In the Main method, add the following statements:

     // Create a proxy object and connect to the service ProductsServiceClient proxy =      new ProductsServiceClient("BasicHttpBinding_IProductsService");

    The ProductsServiceClient class is the name of the proxy type generated earlier. This code creates a new instance of the proxy and connects to the ProductsService service. The parameter to the constructor, BasicHttpBinding_IProductsService, specifies the name of the endpoint in the image from book app.config file to which the client will connect.

  8. Add the following code to the Main method:

     // Test the operations in the service // Obtain a list of all products Console.WriteLine("Test 1: List all products"); string[] productNumbers = proxy.ListProducts(); foreach (string productNumber in productNumbers) {     Console.WriteLine("Number: " + productNumber); } Console.WriteLine();

    This block of code tests the ListProducts method. This method should return an array of strings containing the product number of every product in the database. The foreach statement iterates through the list and displays them.

  9. Add the following code to the Main method:

     // Fetch the details for a specific product Console.WriteLine("Test 2: Display the details of a product"); Product product = proxy.GetProduct("WB-H098"); Console.WriteLine("Number: " + product.ProductNumber); Console.WriteLine("Name: " + product.Name); Console.WriteLine("Color: " + product.Color); Console.WriteLine("Price: " + product.ListPrice); Console.WriteLine();

    This section of code tests the GetProduct method. The GetProduct method returns the details for the specified product (in this case, product WB-H098) as a Product object. Remember that the definition of the Product type was specified in a data contract for the WCF service. The code defining this type in the client application was generated from the metadata for the service and can be found in the image from book ProductsService.cs file, in the image from book ProductsService.map folder, under Service References in Solution Explorer.

  10. Add the following code to the Main method:

     // Query the stock level of this product Console.WriteLine("Test 3: Display the stock level of a product"); int numInStock = proxy.CurrentStockLevel("WB-H098"); Console.WriteLine("Current stock level: " + numInStock); Console.WriteLine();

    This block of code tests the CurrentStockLevel method. The value returned should be the total number of product WB-H098 held in the warehouse (the stock might be held in several bins, located on several shelves).

  11. Add the following code to the Main method:

     // Modify the stock level of this product Console.WriteLine("Test 4: Modify the stock level of a product"); if (proxy.ChangeStockLevel("WB-H098", 100, "N/A", 0)) {     numInStock = proxy.CurrentStockLevel("WB-H098");     Console.WriteLine("Stock changed. Current stock level: " + numInStock); } else {     Console.WriteLine("Stock level update failed"); } Console.WriteLine();

    This code tests the ChangeStockLevel method. Product WB-H098 is located on shelf "N/A," in bin 0, and this code adds another 100 to the volume in stock. The code then calls the CurrentStockLevel method again, which should return the new stock level for this product.

  12. Complete the Main method by adding the following code:

     // Disconnect from the service proxy.Close(); Console.WriteLine("Press ENTER to finish"); Console.ReadLine();

    You disconnect from a service by calling the Close method of the proxy. You should not attempt to call further methods by using the proxy without connecting again.

  13. Save the project, and build the solution.

The final step is to run the client application and verify that the service operates as expected.

Run the client application

  1. In Solution Explorer, right-click the ProductsClient project, and then click Set as Startup Project.

  2. On the Debug menu, click Start Without Debugging.

    A console window opens. A list of product numbers should appear first, followed by the details of product WB-H098 (a water bottle), the current stock level (252), and the stock level after adding another 100 (352):

    image from book

  3. Press Enter to terminate the program and return to Visual Studio 2005.




Microsoft Windows Communication Foundation Step by Step
Microsoft Windows Communication Foundation Step by Step (Step By Step Developer Series)
ISBN: 0735623368
EAN: 2147483647
Year: 2007
Pages: 105
Authors: John Sharp

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net