The final step in this tutorial is to build a client application that will call the WCF service. In the Solution Explorer, right-click the IntroToWCFDemo solution and select Add | New Project to display the Add New Project dialog box. SelectWindows in the Project Types list and Windows Forms Application in the Templates list. Name the projectWindowsClient and click OK. In the solution explorer, right-click WindowsClient and select Set as Startup Project.
You will next add a reference to the service so you can call its methods. In the Solution Explorer, right-click on WindowsClient and select Add ServiceReference to display the Add Service Reference dialog box. Click Discover to search for services that the current solution contains.
You can also enter the service’s URL in the Address text box and click Go.
In the Services pane, select WebHost/InventoryService.svc. Expand the WebHost/InventoryService.svc node. Expand the InventoryService node and
select IInventoryService. You should see the available operations/methods in the Operations pane (see Figure 9).
Figure 9 - Select the WCF service that you want to use.
Two services appear in the dialog box because in the solution there are two configuration files that define endpoints. The first is the app.config file in the WCF service library project. The WCF Service Host and WCF Test Client applications use the endpoints defined in that
configuration file. The second configuration file is the Web.config file in the WebHost project you just created.
Enter InventoryService in the Namespace text box and click OK. In the Solution Explorer, double-click the WindowsClient project’s app.config file. You should see the following XML:
<client>
<endpoint
address="http://localhost:2831/WebHost/InventoryService.svc"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IInventoryService"
contract="InventoryService.IInventoryService"
name="WSHttpBinding_IInventoryService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
Notice that the address, binding and contract match the address, binding and contract defined in the WebHost project’s Web.config file. The client and service can communicate only if the endpoints match.
In the Solution Explorer, delete Form1. Right-click the project and select Add Existing Item to display the Add Existing Item dialog box. Navigate to the folder where you downloaded the tutorial’s sample code. Navigate to the Starter Code folder and select Form1.cs or Form1.vb.
Click Add to add the form. If you are doing this tutorial in C#, open the program.cs file in the WindowsClient project and add the following using statement to the top of the file.
using WindowsFormsApplication15
You do not need to do this for a VB project. This statement is needed in C# because the Form class you just added is defined in the WindowsFormsApplication15 namespace. In VB the class is in the default namespace.
In the Solution Explorer, double-click Form1, opening the form in the form designer (see Figure 10).
Figure 10 - Use this form to call the WCF service.
Double-click the form’s title bar, creating the Form1_Load event handler. At the top of the file, add the following statements:
using System.ServiceModel;
using WindowsClient.InventoryService;
Imports System.ServiceModel
Imports WindowsClient.InventoryService
In the form’s class, add the following variable declarations:
private Product product = null;
private InventoryServiceClient proxy = null;
private decimal inStockValue = 0M;
private decimal onOrderValue = 0M;
Private _product As Product = Nothing
Private proxy As InventoryServiceClient = Nothing Private inStockValue As Decimal = 0D
Private onOrderValue As Decimal = 0D
When you added the Service Reference, Visual Studio generated the InventoryServiceClient class. This is a proxy class you can use to call the WCF service. The operations of the service appear as methods of the proxy class.
In the Form1_Load event handler, add the following code to create a new instance of the proxy class:
proxy = new InventoryServiceClient(
"WSHttpBinding_IInventoryService");
proxy = New InventoryServiceClient( _ "WSHttpBinding_IInventoryService")
In the code above, you pass to the proxy class the name of the endpoint in the app.config file.
This ensures you are using the correct address, binding and contract.
Select View | Designer. Double-click the Get in stock button, creating the Click event handler.
In the Click event handler, add the following code to retrieve the units in stock for a product:
inStockLabel.Text = string.Format("{0} units are in stock", proxy.GetInStock(Convert.ToInt32(productIdTextBox.Text)));
inStockLabel.Text = String.Format("{0} units are in stock", _ proxy.GetInStock(Convert.ToInt32(productIdTextBox.Text)))
Select View | Designer. Double-click the Get product button, creating the Click event handler.
In the Click event handler, add the following code to retrieve additional information for a product:
product = new Product();
product = proxy.GetProduct(
Convert.ToInt32(productIdTextBox.Text));
productNameLabel.Text = product.ProductName;
unitPricelabel.Text = product.UnitPrice.ToString("C");
inStockTextBox.Text = product.UnitsInStock.ToString();
inStockValue=product.UnitPrice *
Convert.ToDecimal(product.UnitsInStock);
inStockValueLabel.Text = inStockValue.ToString("C");
onOrderTextBox.Text = product.UnitsOnOrder.ToString();
onOrderValue=product.UnitPrice *
Convert.ToDecimal(product.UnitsOnOrder);
onOrderValueLabel.Text = onOrderValue.ToString("C");
_product = New Product _product = proxy.GetProduct( _
Convert.ToInt32(productIdTextBox.Text))
productNameLabel.Text = _product.ProductName unitPricelabel.Text = _product.UnitPrice.ToString("C") inStockTextBox.Text = _product.UnitsInStock.ToString() inStockValue = _product.UnitPrice * _
Convert.ToDecimal(_product.UnitsInStock)
inStockValueLabel.Text = inStockValue.ToString("C") onOrderTextBox.Text = _product.UnitsOnOrder.ToString() onOrderValue = _product.UnitPrice * _
Convert.ToDecimal(_product.UnitsOnOrder)
onOrderValueLabel.Text = onOrderValue.ToString("C")
Select View | Designer. Double-click the Update product button, creating the Click event handler. In the Click event handler, add the following code to update inventory information for a product:
product.UnitsInStock = Convert.ToInt16(inStockTextBox.Text);
product.UnitsOnOrder = Convert.ToInt16(onOrderTextBox.Text);
if (proxy.UpdateProduct(product)) {
MessageBox.Show("Your changes were saved");
inStockValue = product.UnitPrice *
Convert.ToDecimal(product.UnitsInStock);
inStockValueLabel.Text = inStockValue.ToString("C");
onOrderValue = product.UnitPrice *
Convert.ToDecimal(product.UnitsOnOrder);
onOrderValueLabel.Text = onOrderValue.ToString("C");
} else {
MessageBox.Show("Your changes were not saved");
}
_product.UnitsInStock = Convert.ToInt16(inStockTextBox.Text) _product.UnitsOnOrder = Convert.ToInt16(onOrderTextBox.Text) If proxy.UpdateProduct(_product) Then
MessageBox.Show("Your changes were saved") inStockValue = _product.UnitPrice * _
Convert.ToDecimal(_product.UnitsInStock)
inStockValueLabel.Text = inStockValue.ToString("C") onOrderValue = _product.UnitPrice * _
Convert.ToDecimal(_product.UnitsOnOrder)
onOrderValueLabel.Text = onOrderValue.ToString("C") Else
MessageBox.Show("Your changes were not saved") End If
Save your changes and build the solution. Press F5 to run the application. In the Manage Inventory form, enter 1 in the Product text box and click Get in stock. The form calls the GetInStock method of the WCF service and displays the units in stock on the form (see Figure 11).
Figure 11 - You called the WCF service to retrieve the units in stock for a product.
Click Get product. The form calls the GetProduct method of the WCF service, passing in a new instance of the Product class. The service retrieves the product’s information, populates the
Product object and returns it to the client, which then displays the product information on the form (see Figure 12).
Figure 12 - You called the WCF service to retrieve additional information for a product.
Enter 10 in the On Order text box and click Update product. Dismiss the dialog box informing you your changes were saved. Close the form. Press F5 to run the application again. In the Manage Inventory form, enter 1 in the Product text box and click Get product. You should see that 10 units are on order for this product. Close the form. To stop the Web server, right-click the ASP.NET Development Server icon in the Notification Area and select Stop (see Figure 13).
Figure 13 - Shut down the ASP.NET Development Server.
Conclusion
This tutorial provided you with an introduction to Windows Communication Foundation (WCF), a unified programming model for building service-oriented applications. You first saw an
overview of how WCF is a unified programming model and what it means to be a service-oriented application. You then built, hosted, and called a WCF service.
If you have used Web services, the techniques you saw in this tutorial should be very familiar. It was clearly a design goal of Visual Studio to make it easy to build and call WCF services. What you haven’t seen yet is how WCF differs from Web services. In upcoming tutorials in this series, you will see things like how to host a WCF service in managed applications as well as Web servers, how to communicate with services using TCP in addition to HTTP and how to easily switch from one protocol to another.