WCF Using NetTcpBinding

This article is about implementing WCF using NetTcpBinding. NetTcpBinding can be useful where IIS services are not needed.
 

How WCF NetTcpBinding works?

  1. The ServiceHost class has been used to configure and expose the service for use by the client application.
     
  2. AddServiceEndPoint method of ServiceHost Class

    It adds a service endpoint to the hosted service with a specified contract (interface), binding (object of NetTcpBinding), and a URI that contains the endpoint address.
     
  3. Open method launches the Service.
     
  4. EndPointAddress class

    Provides a unique network address that a client uses to communicate with a service endpoint.

    It is specified both in the server and the client and must be the same.
     
  5. ChannelFactory class

    It is a generic class that creates and manages the channels that are used by clients to send messages to service endpoints.
For this, I have taken 3 applications
  1. Class Library for creating the business component.
  2. One Windows Application to provide the hosting environment.
  3. Another Windows Application that will act as the client.
The A, B, C's of WCF have been configured manually using the appropriate classes.
 
System.ServiceModel.dll has been included and the appropriate namespaces and classes have been included to provide the implementation.
 
1. Business Component
 
The code for the Business Component has been defined in 2 files. First one is the interface file and second one is the Class file that implements the interface logic.
 
The name of the class library project was remlbb.
 
Add reference to System.ServiceModel.dll
 
Code of the interface
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ServiceModel;  
  6. namespace remlbb  
  7. {  
  8.     [ServiceContract]  
  9.     public interface myinterface  
  10.     {  
  11.         [OperationContract]  
  12.         string Upload(string s);  
  13.   
  14.     }  
  15. }  
Delete the default class definition.
 
Code of the class providing the implementation
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace remlbb  
  7. {  
  8.     public class implementclass : myinterface  
  9.     {  
  10.         public string Upload(string s)  
  11.         {  
  12.             return "hello" + "--" + s;  
  13.    
  14.         }  
  15.     }  
  16. }  
Build the project. It will create remlbb.dll
 
The Server Application for providing the Hosting environment
  • Give your own name to the application. I named it WindowsFormsApplication7mar.
  • Take 2 buttons and change their Text to Start and Stop
  • Add reference to System.ServiceModel.dll and remlbb.dll
The snapshot of the exe of Server Application looks as follows
 
WCF1.gif 
 
Type this code in the Form1.cs file
  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. using remlbb;  
  11. namespace WindowsFormsApplication7mar  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         ServiceHost host;  
  16.         public Form1()  
  17.         {            InitializeComponent();  
  18.         }  
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.             host = new ServiceHost(typeof(implementclass));  
  22.             NetTcpBinding binding = new NetTcpBinding();  
  23.             host.AddServiceEndpoint(typeof(myinterface), binding, new Uri("net.tcp://localhost:5000/implementclass"));  
  24.             host.Open();  
  25.             MessageBox.Show("started");  
  26.             button1.Enabled = false;  
  27.             button2.Enabled = true;  
  28.         }  
  29.         private void button2_Click(object sender, EventArgs e)  
  30.         {  
  31.             host.Close();  
  32.             button1.Enabled = true;  
  33.             button2.Enabled = false;  
  34.         }  
  35.     }  
  36. }  
Client Application
  • Give your own name to a Windows application. I named it WindowsFormsApplication1.
  • Take 1 button and 1 TextBox.
  • Add reference to System.ServiceModel.dll and remlbb.dll
The snapshot of the exe of Client Application looks like this
 
WCF2.gif 
 
The code of the Client file is like this (Form1.cs)
  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. using remlbb;  
  11. namespace WindowsFormsApplication1  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         myinterface idd;  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.         private void Form1_Load(object sender, EventArgs e)  
  21.         {  
  22.             NetTcpBinding binding = new NetTcpBinding();  
  23.             EndpointAddress addr = new EndpointAddress("net.tcp://localhost:5000/implementclass");  
  24.             ChannelFactory<myinterface> chn = new ChannelFactory<myinterface>(binding, addr);  
  25.             idd = chn.CreateChannel();  
  26.         }  
  27.         private void button1_Click(object sender, EventArgs e)  
  28.         {  
  29.             string y = idd.Upload(textBox1.Text);  
  30.   
  31.             MessageBox.Show(y);  
  32.         }  
  33.     }  
  34. }  
To Execute
  1. Click the exe of the server application
  2. Click the start button
  3. Click the exe of the client application
  4. Enter some text in the text box and press the Button. It should display the text entered in a textbox concatenated with Hello.
  5. To stop the service, click the stop button in the exe of server application
Happy Coding!


Similar Articles