Wcf using NetTcpBinding

Ddd
Posted by in WCF category on for Beginner level | Points: 250 | Views : 39733 red flag

A Simple application to implement Wcf using NetTcpBinding


 Download source code for Wcf using NetTcpBinding

Introduction

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

How it works? 

 
1)The ServiceHost  class  has been used  to configure and expose the service for use by the client application.
 
2)Add ServiceEndPoint  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 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
namespace remlbb 

    [ServiceContract] 
    public interface myinterface 
    { 
        [OperationContract] 
  
        string Upload(string s); 
  
    } 
}
 
 
Delete the default class definition. Code of the class providing the implementation
 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
  
namespace remlbb 

    public class implementclass : myinterface 
    { 
        public string Upload(string s) 
        { 
            return "hello" + "--" + s; 
  
        } 
    } 
 
Build the project. It would create remlbb.dll   
 
  • The Server Application for providing the Hosting environment Give your own name to the application. I named it as WindowsFormsApplication7mar. 
  • Take 2 buttons and change their Text to Start and Stop 
  • Add reference to System.ServiceModel.dll and remlbb.dll   

Type this code in the  Form1.cs file

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 remlbb; 
  
namespace WindowsFormsApplication7mar 

    public partial class Form1 : Form 
    { 
        ServiceHost host; 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
  
        private void button1_Click(object sender, EventArgs e) 
        { 
            host = new ServiceHost(typeof(implementclass)); 
            NetTcpBinding binding = new NetTcpBinding(); 
            host.AddServiceEndpoint(typeof(myinterface), binding, new Uri("net.tcp://localhost:5000/implementclass"));
            
  
            host.Open(); 
            MessageBox.Show("started"); 
            button1.Enabled = false; 
            button2.Enabled = true; 
            
        } 
  
        private void button2_Click(object sender, EventArgs e) 
        { 
            host.Close(); 
  
            button1.Enabled = true; 
            button2.Enabled = false; 
  
  
        } 
    } 
}
 
 
Client Application
 
  • Give your own name to  a Windows application. I named it as WindowsFormsApplication1. 
  • Take 1 button and  1 TextBox.  
  • Add reference to System.ServiceModel.dll and remlbb.dll 
 
 The code of the  Client  is like this (Form1.cs)
 
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 remlbb; 
  
namespace WindowsFormsApplication1 

    public partial class Form1 : Form 
    { 
        myinterface idd; 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
  
        private void Form1_Load(object sender, EventArgs e) 
        { 
            NetTcpBinding binding = new NetTcpBinding(); 
  
            EndpointAddress addr = new EndpointAddress("net.tcp://localhost:5000/implementclass"); 
  
            ChannelFactory<myinterface> chn = new ChannelFactory<myinterface>(binding, addr); 
  
            idd = chn.CreateChannel(); 
        } 
  
        private void button1_Click(object sender, EventArgs e) 
        { 
            string y = idd.Upload(textBox1.Text); 
  
            MessageBox.Show(y); 
        } 
    } 
 
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 !
Page copy protected against web site content infringement by Copyscape

About the Author

Ddd
Full Name: dinesh duggal
Member Level: Silver
Member Status: Member
Member Since: 1/4/2011 3:10:44 AM
Country: India
dinesh k.
http://www.dotnetfunda.com
B.E, M.C.S.D.(.NET) .NET TRAINING EXPERIENCE OF AROUNDN 8 YEARS

Login to vote for this post.

Comments or Responses

Posted by: mohanchug-7980 on: 1/10/2011 | Points: 25
Hi Dinesh !

Thanks for this article.
One thing which I don't understand is that client app also has reference to the Business Component (the WCF library).
is there any way to avoid this direct reference of this dll.
and this dll is only present on server side?

Thanks in Advance.

Login to post response

Comment using Facebook(Author doesn't get notification)