When a service and a client both are of .Net and reside on different machines then we can choose the preconfigured netTcpBinding to publish a WCF Service to the client.
netTcpBinding is best suited when both the client and the service are of .Net and communicating to each other either over Intranet or Internet.
Since both the service and client are in .Net, performance of this binding is very much optimized for the best performance. There is no overload of interoperability.
netTcpBinding uses TCP Protocol and Binary Encoding. A few default properties of netTcpBinding is listed in the diagram below.
Now let us create a WCF service using netTcpBinding. The following is what we are going to do.
- Create a WCF Service
- Host Service in Console Application
- Configure EndPoint with netTcpBinding
- 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.
IService1.cs
using System.ServiceModel;
namespace WcfService10
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetDataUsingnetTcpBinding(int value);
}
}
Implement the service as below.
Service1.svc.cs
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 the WCF Service we are going to create a console application. Just create a console application. Add the references below to the console application project.
Add the project reference of WCF Service application project we created in the 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 the previous step.
After adding all required references and the WCF Service project, we are going to write code to host the application.
Program.cs
using System;
using System.ServiceModel;
namespace HostApplication
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(WcfService10.Service1));
host.Open();
Console.WriteLine("Press a key to close the host");
Console.ReadKey(true);
host.Close();
}
}
}
In the code shown above, we are creating an 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 the General tab.
App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfService10.Service1" behaviorConfiguration ="netTcpServiceBehavior">
<endpoint address ="" binding ="netTcpBinding" contract ="WcfService10.IService1" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses >
<add baseAddress ="net.tcp://localhost/SampleNetTCPService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="netTcpServiceBehavior">
<serviceMetadata httpGetEnabled="false" httpGetUrl=""/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors >
</behaviors >
</system.serviceModel>
</configuration>
In the configuration file above:
- We are creating a service behavior. In the service behavior service, we are configuring metadata that will not get enabled over Http.
<behaviors>
<serviceBehaviors>
<behavior name="netTcpServiceBehavior">
<serviceMetadata httpGetEnabled="false" httpGetUrl=""/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors >
</behaviors >
- Attaching the service behavior with service
<service name="WcfService10.Service1" behaviorConfiguration ="netTcpServiceBehavior">
- Creating base address. Client is able to consume the service on address net.tcp://localhost/sampleNetTCPService
<host>
<baseAddresses >
<add baseAddress ="net.tcp://localhost/SampleNetTCPService"/>
</baseAddresses>
</host>
- Creating the EndPoints. First EndPoint is for service contract IService1 and the other endpoint is for metadata.
<endpoint address ="" binding ="netTcpBinding" contract ="WcfService10.IService1" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
Run the console application
Now the service is up and running.
Consume service at console client
Since our service is up and running now is the time to consume the service. Create a console application project; right click the project and add the service reference. For the address you need to provide the same address we configured in App.Config file of host console application project.
Note: You need to make sure host console application project is running otherwise you will get an error while adding service reference.
After successfully adding the service reference we can call the service in the normal way.
Program.cs
using System;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
string result = proxy.GetDataUsingnetTcpBinding(99999);
Console.WriteLine(result);
Console.ReadKey(true);
}
}
}
On running we will get the output as below.
In this article we saw how we can use netTcpBinding for communication between two .Net Applications residing on different machines.