Customizing the client

In the previous sections, we added the test client to the host model, and generated the proxy files for the client application from the host model. However, the test client currently contains only an empty Windows Form, one proxy file, and one configuration file.

So, the last step is to customize the client application. Service Factory has added the service reference, and populated the configuration file for us. What is left for us to do is to add code to call the service, and display the result.

First, we will customize the MainForm of this client application:

  1. Open the MainForm.cs file, change the Search label text to Product ID.
  2. Add a label below the product ID textbox, with the text, Product Details.
  3. Add a textbox control below the product details label, and name it txtResult.

The form should look like this:

Customizing the client

Now, we need to follow the steps below to add the code in the Form.

  1. Double-click on the Execute button to add an event handler. This will also bring the code for the MainForm to the front.
  2. First, add two using statements for the EasyNorthwindProxy and ServiceModel namespaces, like this:
    using MyWCF.EasyNorthwind.Client.EasyNorthwindProxy;
    using System.ServiceModel;
    
  3. Then, modify the ExecuteButton_Click method to call the service to retrieve the product details, and display the result.

The content of the MainForm.cs file is like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyWCF.EasyNorthwind.Client.EasyNorthwindProxy;
using System.ServiceModel;
namespace MyWCF.EasyNorthwind.Client
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void ExecuteButton_Click(object sender, EventArgs e)
{
ProductServiceContractClient client = new ProductServiceContractClient();
GetProductRequest request = new GetProductRequest();
string result = "";
try
{
request.ProductID = Int32.Parse(SearchText.Text. ToString());
Product product = client.GetProduct(request);
StringBuilder sb = new StringBuilder();
sb.Append("ProductID:" + product.ProductID.ToString() + "
");
sb.Append("ProductName:" + product.ProductName + "
");
sb.Append("QuantityPerUnit:" + product.QuantityPerUnit + "
");
sb.Append("UnitPrice:" + product.UnitPrice. ToString("C") + "
");
sb.Append("Discontinued:" + product.Discontinued. ToString());
result = sb.ToString();
}
WCF serviceclient, customizingcatch (TimeoutException ex)
{
result = "The service operation timed out. " + ex.Message;
}
catch (FaultException<ProductFault> ex)
{
result = "ProductFault returned: " + ex.Detail.FaultMessage;
}
catch (FaultException ex)
{
result = "Unknown Fault: " + ex.ToString();
}
catch (CommunicationException ex)
{
result = "There was a communication problem. " + ex.Message + ex.StackTrace;
}
catch (Exception ex)
{
result = "Other excpetion: " + ex.Message + ex.StackTrace;
}
txtResult.Text = result;
}
}
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset