Introduction:
In this article,i am going to explain about how to access a wcf service with adding the service reference.
Main:
We can easily perform this task using channel factory.A channelfactory is a class that helps us to
creates channels of different types that are used by clients to send messages to variously configured
service endpoints.
Demonstration:
1.make a wcf service is hosted and running,

2.Declare the same interface in client side,

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;
using System.ServiceModel.Channels;
using NetTcpLibarary;
namespace NetTcpClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[ServiceContract]
interface INetTcpService
{
[OperationContract()]
DataSet ReturnEmpDetails(int EmpId);
}
private void Form1_Load(object sender, EventArgs e)
{
NetTcpBinding basicHttpBinding = new NetTcpBinding();
EndpointAddress endpointAddress = new EndpointAddress
("net.tcp://localhost:8000/TcpBinding");
IChannelFactory<INetTcpService> channelFactory =
new ChannelFactory<INetTcpService>(basicHttpBinding);
// IChannelFactory<INetTcpService> local_channelfactory =
// new IChannelFactory<INetTcpService>(basicHttpBinding);
INetTcpService proxy = channelFactory.CreateChannel(endpointAddress);
DataSet local_ds = proxy.ReturnEmpDetails(5303);
if (local_ds.Tables[0].Rows.Count > 0)
{
MessageBox.Show(local_ds.Tables[0].Rows[0].ItemArray[0].ToString());
}
//dataGridView1.DataSource = local_ds;
// dataGridView1.DataBindings();
}
}
}
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; using System.ServiceModel.Channels; using NetTcpLibarary; namespace NetTcpClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } [ServiceContract] interface INetTcpService { [OperationContract()] DataSet ReturnEmpDetails(int EmpId); } private void Form1_Load(object sender, EventArgs e) { NetTcpBinding basicHttpBinding = new NetTcpBinding(); EndpointAddress endpointAddress = new EndpointAddress ("net.tcp://localhost:8000/TcpBinding"); IChannelFactory<INetTcpService> channelFactory = new ChannelFactory<INetTcpService>(basicHttpBinding); // IChannelFactory<INetTcpService> local_channelfactory = // new IChannelFactory<INetTcpService>(basicHttpBinding); INetTcpService proxy = channelFactory.CreateChannel(endpointAddress); DataSet local_ds = proxy.ReturnEmpDetails(5303); if (local_ds.Tables[0].Rows.Count > 0) { MessageBox.Show(local_ds.Tables[0].Rows[0].ItemArray[0].ToString()); } //dataGridView1.DataSource = local_ds; // dataGridView1.DataBindings(); } } } |
Thantsit!
1 Comments.