Cross machine communication between .Net Application using WCF

No.of Views2272
Bookmarked0 times
By  Dhananjay Kumar   On  08 Mar 2011 07:03:24
Tag : WCF , Utilities
When service and client both are of .Net and resides on different machines then we choose preconfigured netTcpBinding to publsih WCF Service to the client.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at



When service and client both are of .Net and resides on different machines then we choose preconfigured  netTcpBinding to publsih WCF Service to the client.

Image Loading

netTcpBinding is best suited when both client and service are of .Net and communicating to each other either over Intranet or Internet.
Since both the service and client are in .Net, so performance of this binding is very much optimized for best. There is no overload of interoperability.
netTcpBinding uses  TCP Protocol and Binary Encoding . Few deafult properties  of netTcpBinding is listed in below digram. 

Image Loading

Now let us create a WCF service using netTcpBinding . what we are going to do

1.    Create a WCF Service
2.    Host Service in Console Application
3.    Configure  EndPoint with netTcpBinding
4.    Consume service at console client.

Create WCF Service

Open Visual Studio 2010 and create a new project by selecting WCF Service Aplication project template in WCF tab. If you are using Visual Studio 2008 then select WCF Service Application from Web Tab.
Now let us create a very simple Service Contract


using System.ServiceModel;
namespace WcfService10
    public interface IService1
        string GetDataUsingnetTcpBinding(int value);

Implement service as below,

namespace WcfService10
    public class Service1 : IService1
        public string GetDataUsingnetTcpBinding(int value)
            return string.Format("You are fetching: {0} using netTcpbinding", value);

Host Service in Console Application

To host WCF Service we are going to create console application. Just create a console application. Add below references in console application project,

Image Loading

Add the project reference of WCF Service application project we created in previous step.  Right click on console application project and select Add Reference then click on Project tab and browse to WCF Service application project we created in previous step.

Image Loading

After adding all required references and of WCF Service project, we are going to write a code to host the application.

using System;
using System.ServiceModel; 
namespace HostApplication
    class Program
        static void Main(string[] args)
            ServiceHost host = new ServiceHost(typeof(WcfService10.Service1));
            Console.WriteLine("Press a key to close the host");

In above code, we are creating instance of ServiceHost, opening the host and closing that.

Configure  EndPoint with netTcpBinding

Now we need to configure EndPoint with netTcpBinding.   Right click and add new item then choose Application Configuration file from General tab.


<?xml version="1.0" encoding="utf-8" ?>
      <service name="WcfService10.Service1" behaviorConfiguration ="netTcpServiceBehavior">
        <endpoint address ="" binding ="netTcpBinding" contract ="WcfService10.IService1" />
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
          <baseAddresses >
            <add baseAddress ="net.tcp://localhost/SampleNetTCPService"/>
        <behavior name="netTcpServiceBehavior">
          <serviceMetadata httpGetEnabled="false" httpGetUrl=""/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
      </serviceBehaviors >
    </behaviors >

In above configuration file

1.    We are creating service behavior. In service behavior service, we are configuring metadata will not get enabled through over Http. 

Image Loading

2.    Attaching the service behavior with service 

Image Loading

3.    Creating base address. Client will able to consume the service on address net.tcp://localhost/sampleNetTCPService

Image Loading

4.    Creating the EndPoints.  First EndPoint is for service contract IService1 and other endpoint is for metadata. 


Image Loading

Run the console application

Image Loading

Now service is up and running.

Consume service at console client
Since our service is up and running this is time to consume the service. Create a console application project right click and add service reference.  In address you need to provide the same address we configured  in App.Config file of host console application project.

Image Loading

Note:  You need to make sure host console application project is running else you will get error while adding service reference.
After successfully adding the service reference we can call the service in normal way


using System;
namespace ConsoleApplication10
    class Program
        static void Main(string[] args)
            ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
            string result = proxy.GetDataUsingnetTcpBinding(99999);

On running we will get the output as below ,

Image Loading

In this article we saw how could we use netTcpBinding for communication between two .Net Application residing on different machines.

Sign Up to vote for this article
About Author
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Joined date-20 Jul 2009
Home Page-
Blog Page-
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at . You can follow him and reach him at
Other popularSectionarticles
There is no comments for this articles.
Leave a Reply
Display Name:
(not display in page for the security purphase)
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top