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);
}
}
Task 3: In the client project add a new App.Config file and write the following code:
<?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>
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:
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();
}
}
}
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:
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();
}
}
}
The changes are denoted using the highlighting color.
Task 3: Run the application; you will get output.