Programmatically Creating Proxy of the WCF Service Client Application Without Adding Service Reference

In this exercise we will see how to design a proxy using APIs provided by WCF. We have seen the "ServiceHost<T>" class used to host a WCF service. The class "ClientBase<T>" is used as a base type for creating a proxy class in the client application. The "ChannelFactory<T>" class channelizes requests from a client application to the WCF service. These classes are in the "System.ServiceModel" namespace. You can use the mechanism of programmatic proxy creation, since all clients are .NET clients and you are working in an intranet environment.

In this exercise the first solution will use "ChannelFactory<T>" and "ClientBase<T>".

Task 1: Open the solution created in Exercise 1. Remove the Service Reference and the App.Config file from the client project.

Task 2: Copy the "IService.cs" interface from the "WCF_Service" project to the Windows client project and change the namespace to the Windows Client project as below:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using System.ServiceModel;  
  7. namespace WinForm_WCFClinet  
  8. {  
  9.     [ServiceContract]  
  10.     public interface IService  
  11.     {  
  12.         [OperationContract]  
  13.         int Add(int x, int y);  
  14.         [OperationContract]  
  15.         DataSet GetData(string dbName, string tbName);  
  16.     }  
  17. }  
Task 3: In the client project add a new App.Config file and write the following code: 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <client>  
  5.       <endpoint name="ClientEdp"  
  6.       address="http://localhost:9012/MyServ"  
  7.       binding="basicHttpBinding" contract="WinForm_WCFClinet.IService"></endpoint>  
  8.     </client>  
  9.   </system.serviceModel>  
  10. </configuration>  
Note that the client uses the same address and binding. The name of the attribute of the endpoint is used by the client application to subscribe to the channel published by the WCF service.

Task 4: Open Form1.cs and write the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.ServiceModel;  
  10. namespace WinForm_WCFClinet  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         IService Proxy; //The interface reference  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         private void btnAdd_Click(object sender, EventArgs e)  
  20.         {  
  21.             MessageBox.Show(Proxy.Add(2, 4).ToString());  
  22.         }  
  23.         private void btnGetdadat_Click(object sender, EventArgs e)  
  24.         {  
  25.             DataSet Ds = Proxy.GetData(txtdbname.Text, txttbname.Text); dgtable.DataSource = Ds.Tables[txttbname.Text];  
  26.         }  
  27.         private void Form1_Load(object sender, EventArgs e)  
  28.         {  
  29.             //Creates Channel by reading Endpoint Proxy = new  
  30.             ChannelFactory<IService>("ClientEdp").CreateChannel();  
  31.         }  
  32.     }  
  33. }  
Task 5: Run the application, the application is running, in other words you will get output after clicking the "Add" and "GetData" buttons.

In the following tasks we will modify the client application. We will create a proxy class that is inherited from the "ClientBase<T>" class.

Task 1: In the client project add a new class, name it as "ProxyClass" and write the code as below:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ServiceModel;  
  6. namespace WinForm_WCFClinet  
  7. {  
  8.     public class ProxyClass : ClientBase<IService>, IService  
  9.     {  
  10.         #region IService Members  
  11.         public int Add(int x, int y)  
  12.         {  
  13.             return base.Channel.Add(x, y);  
  14.         }  
  15.         public System.Data.DataSet GetData(string dbName, string tbName)  
  16.         {  
  17.             return base.Channel.GetData(dbName, tbName);  
  18.         }  
  19.         #endregion  
  20.     }  
  21. }  

Task 2: Change the Form1.cs code as below: 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.ServiceModel;  
  10. namespace WinForm_WCFClinet  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         ProxyClass Proxy;  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         private void btnAdd_Click(object sender, EventArgs e)  
  20.         {  
  21.             MessageBox.Show(Proxy.Add(2, 4).ToString());  
  22.         }  
  23.         private void btnGetdadat_Click(object sender, EventArgs e)  
  24.         {  
  25.             DataSet Ds = Proxy.GetData(txtdbname.Text, txttbname.Text); dgtable.DataSource = Ds.Tables[txttbname.Text];  
  26.         }  
  27.         private void Form1_Load(object sender, EventArgs e)  
  28.         {  
  29.             //Creates Proxy object using Proxyclass Proxy = new ProxyClass();  
  30.         }  
  31.     }  
  32. }  
The changes are denoted using the highlighting color.

Task 3: Run the application; you will get output.


Similar Articles