Cross machine communication between .Net Application using WCF

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.

 

clip_image002

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.

clip_image004

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,

clip_image006

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.

clip_image008

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.

 

clip_image010

2. Attaching the service behavior with service

 

clip_image012

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

clip_image014

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

 

clip_image015

Run the console application

clip_image017

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.

clip_image018

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 ,

clip_image020

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

About these ads

Programmatically finding Binding Elements in WCF Binding

Binding is one of the important aspect of WCF Programming. It deals with, “How Service and client will talk to each other?” Binding is made up of different binding elements. When different binding elements are stacked together they form a Binding. Different binding types are made up of different binding elements.

Always a question arises that, “What are the different binding elements of a particular binding? “

And we can find solution of above question through code. What all we need to do

1. Create instance of Binding

2. Enumerate through Binding elements.

We are going to create console application to enumerate through all the binding elements in a binding.

Add below references in console application project,

clip_image001

We are going to write a function. This function will take object of a Binding as input parameter and prints all the binding elements.

 

clip_image003

Above function is very simple, just enumerating above all the binding elements.

We may call above function as below,

clip_image004

Full source code is as below,

Program.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels; 

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {

            EnumerateBindingElements(new BasicHttpBinding());
            EnumerateBindingElements(new NetMsmqBinding());
            EnumerateBindingElements(new WSHttpBinding());
            Console.ReadKey(true);

        }

        static void EnumerateBindingElements(Binding binding)
        {
            Console.WriteLine("Binding : " + binding.GetType().Name);
            BindingElementCollection bindingElements = binding.CreateBindingElements();
            foreach (BindingElement e in bindingElements)
            {
                Console.WriteLine(e.GetType().FullName);
            }          

        }
    }
}


 

Output

clip_image006

Windows Azure For Developers Task 5: Configuring Virtual Machine sizes

What is specifying Virtual Machine Size?

We can specify the size of virtual machine on which Role will be deployed. Size of VM defines

1. CPU core

2. Memory capacity

3. Local file system

We can specify Virtual Machine size for the Role.

There are two way to specify the VM size

1. Through Service Definition file

2. If using Visual Studio then through property tab on the Role.

Through Service definition file

Open ServiceDefinition.csdef file and you can choose the size from provided dropdown.

clip_image002

Through Properties tab

Click on Web Role

clip_image003

And you can provide VM size by selecting Configuration tab and choosing VM size from the drop down.

clip_image005

There are 5 options available to choose from for the size of VM. Below table gives detail of all options available with different VM size

clip_image007