Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
BRH 58
#ASP.NET 55
ASP.NET 50
#DOTNET 49
.NET 40
WCF 21
DOTNET 12
c# 8
windows azure 7
SILVERLIGHT 7

Archive · View All
April 2011 9
March 2011 9
February 2011 8
December 2010 7
November 2010 5
September 2010 5
August 2010 5
May 2011 4
October 2010 4
January 2011 2

Cross machine communication between .Net Application using WCF

Mar 15 2011 3:38AM by Dhananjay Kumar   

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.

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.

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

IService1.cs

using System.ServiceModel;
namespace WcfService10
{ 
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetDataUsingnetTcpBinding(int value);
    }   
}

Implement 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 WCF Service we are going to create console application. Just create a console application. Add below references in console application project,

using System.ServiceModel; 
using System.ServiceModel.Channels;

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.

After adding all required references and of WCF Service project, we are going to write a 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 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.

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 above configuration file

  1. We are creating service behavior. In service behavior service, we are configuring metadata will not get enabled through over Http.
    <behaviors>
         <serviceBehaviors>
            <behavior name="netTcpServiceBehavior">
              <serviceMetadata httpGetEnabled="false" httpGetUrl=""/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors >
    </behaviors >
    
  2. Attaching the service behavior with service
    <service name="WcfService10.Service1" behaviorConfiguration ="netTcpServiceBehavior">
  3. Creating base address. Client will able to consume the service on address net.tcp://localhost/sampleNetTCPService
    <host>
        <baseAddresses >
            <add baseAddress ="net.tcp://localhost/SampleNetTCPService"/>
        </baseAddresses>
    </host>
    
  4. Creating the EndPoints. First EndPoint is for service contract IService1 and other endpoint is for metadata.
    <endpoint address=""
              binding="netTcpBinding"
              contract="WcfService10.IService1" />
    <endpoint address="mex"
              binding="mexTcpBinding"
              contract="IMetadataExchange" />
    

Run the console application

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.

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

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 could we use netTcpBinding for communication between two .Net Application residing on different machines.

Tags: WCF, #ASP.NET, ASP.NET, BRH, .NET,


Dhananjay Kumar
49 · 4% · 1198
1
 
0
Lifesaver
 
0
Refreshed
 
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Cross machine communication between .Net Application using WCF" rated 5 out of 5 by 1 readers
Cross machine communication between .Net Application using WCF , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]