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:
MainForm.cs
file, change the Search label text to Product ID.The form should look like this:
Now, we need to follow the steps below to add the code in the Form.
MainForm
to the front. using
statements for the EasyNorthwindProxy
and ServiceModel namespaces
, like this:using MyWCF.EasyNorthwind.Client.EasyNorthwindProxy; using System.ServiceModel;
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; } } }