Introduction:
In this article i am going to explain about how to use nettcpbinding and netnamed
pipebinding in WCF programming.
Main:
Bindings are the mechanism by which communication details are specified to make connecting
to a service’s WCF endpoint,
NetTcpBinding :- Provides a secure and reliable binding environment for .Net to .Net
cross machine communication.It Uses TCP protocol and provides full support for SOAP
security,transaction and reliability.
NetNamedPipeBinding :- The NetNamedPipeBinding provides a secure and reliable binding
environment for cross-process (same machine) communication. It uses the NamedPipe protocol
and provides full support for SOAP security, transactions, and reliability.
Demonstration:
1.Creating WCF Service Library,
Goto Project –> New Project –> WCF –> Select WCF Library and named as NetTcpLibrary,
Paste the below Code,
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace NetTcpLibarary
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface INetTcpService
{
[OperationContract]
DataSet ReturnEmpDetails(int EmpId);
}
// Use a data contract as illustrated in the sample below to add composite types to service operations
public class NetcpService : INetTcpService
{
DataSet INetTcpService.ReturnEmpDetails(int EmpId)
{
SqlConnection conn = new SqlConnection("Data Source=CHANDRU-PC;Initial Catalog=master;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand(string.Format("Select firstname,lastname,deptname,Manager from Emp where empid = {0}", EmpId),conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
using System; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace NetTcpLibarary { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. [ServiceContract] public interface INetTcpService { [OperationContract] DataSet ReturnEmpDetails(int EmpId); } // Use a data contract as illustrated in the sample below to add composite types to service operations public class NetcpService : INetTcpService { DataSet INetTcpService.ReturnEmpDetails(int EmpId) { SqlConnection conn = new SqlConnection("Data Source=CHANDRU-PC;Initial Catalog=master;Integrated Security=True"); conn.Open(); SqlCommand cmd = new SqlCommand(string.Format("Select firstname,lastname,deptname,Manager from Emp where empid = {0}", EmpId),conn); SqlDataAdapter da = new SqlDataAdapter(cmd); cmd.ExecuteNonQuery(); DataSet ds = new DataSet(); da.Fill(ds); return ds; } } } |
2.Host the WCF service,
Create one windows application and named it HostingNetTcp,
Refer the Service Library we created,(Goto Add Reference and refer NetTcpLibrary.dll)
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.Description;
using NetTcpLibarary;
namespace HostingNetTcp
{
public partial class Form1 : Form
{
ServiceHost local_sh = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Uri local_Namedpipe = new Uri("net.pipe://localhost/NetNamedPipeBinding");
Uri local_tcp = new Uri("net.tcp://localhost:8000/TcpBinding");
local_sh = new ServiceHost(typeof(NetcpService), local_tcp, local_Namedpipe);
NetNamedPipeBinding local_pb = new NetNamedPipeBinding();
NetTcpBinding local_tcpb = new NetTcpBinding();
ServiceMetadataBehavior local_mbehave = new ServiceMetadataBehavior();
local_sh.Description.Behaviors.Add(local_mbehave);
local_sh.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
local_sh.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding(), "mex");
local_sh.AddServiceEndpoint(typeof(INetTcpService), local_pb, local_Namedpipe);
local_sh.AddServiceEndpoint(typeof(INetTcpService), local_tcpb, local_tcp);
local_sh.Open();
label1.Text = "Service Running";
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
local_sh.Close();
}
}
}
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.Description; using NetTcpLibarary; namespace HostingNetTcp { public partial class Form1 : Form { ServiceHost local_sh = null; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Uri local_Namedpipe = new Uri("net.pipe://localhost/NetNamedPipeBinding"); Uri local_tcp = new Uri("net.tcp://localhost:8000/TcpBinding"); local_sh = new ServiceHost(typeof(NetcpService), local_tcp, local_Namedpipe); NetNamedPipeBinding local_pb = new NetNamedPipeBinding(); NetTcpBinding local_tcpb = new NetTcpBinding(); ServiceMetadataBehavior local_mbehave = new ServiceMetadataBehavior(); local_sh.Description.Behaviors.Add(local_mbehave); local_sh.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex"); local_sh.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexNamedPipeBinding(), "mex"); local_sh.AddServiceEndpoint(typeof(INetTcpService), local_pb, local_Namedpipe); local_sh.AddServiceEndpoint(typeof(INetTcpService), local_tcpb, local_tcp); local_sh.Open(); label1.Text = "Service Running"; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { local_sh.Close(); } } } |
3.Create a WCF Client,
Make Host service application is running,
Refer the url’s we created in hosting application,
Create One Windows application and named as NetTcpBindingClient,
using System;
using System.Data;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NetTcpBindingClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
TCP.NetTcpServiceClient objclient = new TCP.NetTcpServiceClient("NetTcpBinding_INetTcpService");
DataSet ds = objclient.ReturnEmpDetails(Convert.ToInt32(textBox1.Text));
if (ds.Tables[0].Rows.Count > 0)
{
label2.Text = ds.Tables[0].Rows[0].ItemArray[0].ToString();
label3.Text = ds.Tables[0].Rows[0].ItemArray[1].ToString();
label4.Text = ds.Tables[0].Rows[0].ItemArray[2].ToString();
label5.Text = ds.Tables[0].Rows[0].ItemArray[3].ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
NetPipe.NetTcpServiceClient objNetClient = new NetPipe.NetTcpServiceClient("NetTcpBinding_INetPipeService");
DataSet Netds = objNetClient.ReturnEmpDetails(Convert.ToInt32(textBox1.Text));
if (Netds.Tables[0].Rows.Count > 0)
{
label2.Text = Netds.Tables[0].Rows[0].ItemArray[0].ToString();
label3.Text = Netds.Tables[0].Rows[0].ItemArray[1].ToString();
label4.Text = Netds.Tables[0].Rows[0].ItemArray[2].ToString();
label5.Text = Netds.Tables[0].Rows[0].ItemArray[3].ToString();
}
}
}
}
using System; using System.Data; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace NetTcpBindingClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { TCP.NetTcpServiceClient objclient = new TCP.NetTcpServiceClient("NetTcpBinding_INetTcpService"); DataSet ds = objclient.ReturnEmpDetails(Convert.ToInt32(textBox1.Text)); if (ds.Tables[0].Rows.Count > 0) { label2.Text = ds.Tables[0].Rows[0].ItemArray[0].ToString(); label3.Text = ds.Tables[0].Rows[0].ItemArray[1].ToString(); label4.Text = ds.Tables[0].Rows[0].ItemArray[2].ToString(); label5.Text = ds.Tables[0].Rows[0].ItemArray[3].ToString(); } } private void button2_Click(object sender, EventArgs e) { NetPipe.NetTcpServiceClient objNetClient = new NetPipe.NetTcpServiceClient("NetTcpBinding_INetPipeService"); DataSet Netds = objNetClient.ReturnEmpDetails(Convert.ToInt32(textBox1.Text)); if (Netds.Tables[0].Rows.Count > 0) { label2.Text = Netds.Tables[0].Rows[0].ItemArray[0].ToString(); label3.Text = Netds.Tables[0].Rows[0].ItemArray[1].ToString(); label4.Text = Netds.Tables[0].Rows[0].ItemArray[2].ToString(); label5.Text = Netds.Tables[0].Rows[0].ItemArray[3].ToString(); } } } } |
Conclusion:
Hope this helps,
Happy Coding.





I would like to exchange links with your site netprogramminghelp.com
Is this possible?
Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.
I am very new to wcf and the .net enviroment. Our WCF person has recently left our company and I have inherited his projects. I don’t want to offend anyone by asking this (and I know this is a sore subject for many programmers) but can this be coverted into vb code easily? We have several services written already in this and I am having to make updates to them. It is for ease of reading on my part….