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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.ServiceModel;
- namespace WinForm_WCFClinet
- {
- [ServiceContract]
- public interface IService
- {
- [OperationContract]
- int Add(int x, int y);
- [OperationContract]
- DataSet GetData(string dbName, string tbName);
- }
- }
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <system.serviceModel>
- <client>
- <endpoint name="ClientEdp"
- address="http://localhost:9012/MyServ"
- binding="basicHttpBinding" contract="WinForm_WCFClinet.IService"></endpoint>
- </client>
- </system.serviceModel>
- </configuration>
Task 4: Open Form1.cs and write the following code:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.ServiceModel;
- namespace WinForm_WCFClinet
- {
- public partial class Form1 : Form
- {
- IService Proxy; //The interface reference
- public Form1()
- {
- InitializeComponent();
- }
- private void btnAdd_Click(object sender, EventArgs e)
- {
- MessageBox.Show(Proxy.Add(2, 4).ToString());
- }
- private void btnGetdadat_Click(object sender, EventArgs e)
- {
- DataSet Ds = Proxy.GetData(txtdbname.Text, txttbname.Text); dgtable.DataSource = Ds.Tables[txttbname.Text];
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //Creates Channel by reading Endpoint Proxy = new
- ChannelFactory<IService>("ClientEdp").CreateChannel();
- }
- }
- }
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- namespace WinForm_WCFClinet
- {
- public class ProxyClass : ClientBase<IService>, IService
- {
- #region IService Members
- public int Add(int x, int y)
- {
- return base.Channel.Add(x, y);
- }
- public System.Data.DataSet GetData(string dbName, string tbName)
- {
- return base.Channel.GetData(dbName, tbName);
- }
- #endregion
- }
- }
Task 2: Change the Form1.cs code as below:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.ServiceModel;
- namespace WinForm_WCFClinet
- {
- public partial class Form1 : Form
- {
- ProxyClass Proxy;
- public Form1()
- {
- InitializeComponent();
- }
- private void btnAdd_Click(object sender, EventArgs e)
- {
- MessageBox.Show(Proxy.Add(2, 4).ToString());
- }
- private void btnGetdadat_Click(object sender, EventArgs e)
- {
- DataSet Ds = Proxy.GetData(txtdbname.Text, txttbname.Text); dgtable.DataSource = Ds.Tables[txttbname.Text];
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //Creates Proxy object using Proxyclass Proxy = new ProxyClass();
- }
- }
- }
Task 3: Run the application; you will get output.

chitra patelPosted Feb 20, 2020, 4:16 PM
What if we don't know about which binding is used in WCF service?
Rohit SinghPosted Feb 12, 2020, 10:53 PM
Can you please give me the link for the entire tutorial Amit sir?? Would love to learn from the beginning!!
Amit TiwariPosted May 31, 2016, 7:58 AM
Thanks
Mukesh KumarPosted Nov 28, 2015, 3:55 AM
Good One