Saturday, 23 June 2012

How WCF supports multile Bindings?

Yes, Windows Communication Foundation(WCF) Supports multiple clients with bindings to improve the performance of service.

like  BasicHttpBinding, WsHttpBinding,NetTcpBinding,MSMQBinding...ect.
why all these bindings?----to improve the performance of data exchange between the applications.
Example:
1. Based on the client.
2. Based on the requirements

1. Based on the Client :means the service implemented wcf can support multiple bindings but the service(wcf) has to provide an endpoint to client.
single service can support multiple endpoints with multiple endpoints.
ex : BasicHttpBinding()-----every one can cousume the service. but doesn't support transactions.
 
a client can be .net, java,php..ect every client can consume.(through the adding reference)[in .net service web reference].

2.Based on the requirement:

evry binding has its own features so as per our requirement suitable binding we have to choose.

Ex: basicHttpBinding doesn' t support Transactions so then go for wsHttpBinding like as per our requirement.
wsHttpBinding also universally accessable binding so everyone consume the reference.

so Finally WCF  Supports good support to all with multiple bindings with multiple endpoints


for .NET clients( consumer application is developed in .net) so adds one more endpoint to same service


 Endpoint for .Net only:

<endpoint address="/first" binding="netTcpBinding" bindingConfiguration=""
          contract="WcftcpServiceLibrary1.IService1">
      
Endpoint for .NET and for everyone:
<endpoint address="/second" binding="BasicTcpBinding" bindingConfiguration=""
          contract="WcftcpServiceLibrary1.IService1">
          <identity>
now dotnet client consume the service with good native of the service.
but
How client knows about our wcf service supporting how many bindings?
yes, good question, whenever client consume the service reference at client. whatever supported bindings and endpoints are automatically added at client.
then 
non of the .net users uses : BasicHttpBinding() for consume of service.
.net users uses                  : netTcpBinding() ;
like this single service supports multile clients with multiple bindings.
NOTE: AS a .net client also uses the BasicHttpBinding() but we lose the performance of service.(which provided by the service).
we can know more things using service hosting using WCF supported selfhosting:
WCF selfHosting:
Yes, WCF supports 3 types of hostings:
1.Self Hosting         2.Windows Hosting           3.IIS Hosting***********

SefHosting:
File—newàConsoleApplication1------create a console application.
Add Interface is--- IMathService.cs

using System.ServiceModel;

namespace ConsoleApplication1
{
    [ServiceContract]
    interface IMathService
    {
        [OperationContract]
        int GetSum(int a, int b);
       
    }
}

Implements the IMathService.cs

namespace ConsoleApplication1
{
    class MathService: IMathService
    {
        public int GetSum(int a, int b)
        {
            return a + b;
        }
    }
}

Service is created Host now using SelfHosting:(same project of Progam.cs)

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {          
using (ServiceHost sh = new ServiceHost(typeof(MathService), new Uri("http://localhost:72/Service"), new Uri("net.tcp://localhost:71/Service")))
{             
  //Adding the endpoints to the ServiceHost
sh.AddServiceEndpoint(typeof(IMathService), new NetTcpBinding(), "");
sh.AddServiceEndpoint(typeof(IMathService), new WSHttpBinding(), "");
            
***  //Creating a new Service Behavior

 // ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
 // smb.HttpGetEnabled = true;
 // sh.Description.Behaviors.Add(smb);***

---for client gets metadata generation
In browse : http://localhost:72/Service who ever adding this reference automatically adds metadata also
If we don’t write the metadata will be disabled.
            
             //Opening the ServiceHost for incoming connections
             sh.Open();
            
             System.Console.WriteLine("Waiting for clients ...");
             System.Console.ReadLine();
            
             //Close opened ServiceHost object
             sh.Close();
             }
        }
    }
}
then console with  Waiting for clients ...
yes, now our service is ready to provide service for .net user (using .net.tcp binding) and non-net user(BasicHttpBinding).

Create Client Application for Communicate with WCF service

using ConsoleApplication2.ServiceReference1;
class Program
    {
        static void Main(string[] args)
        {
            using (ServiceReference1.MathServiceClient proxy = new ServiceReference1.MathServiceClient("NetTcpBinding_IMathService"))
{
proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(new Uri("net.tcp://localhost:71/Service"));
                proxy.Endpoint.Binding = new NetTcpBinding();
                proxy.Endpoint.Contract.ContractType = typeof(IMathService);
                Console.WriteLine("Net.TCP: 1 + 1 is " + proxy.GetSum(1, 1));
                proxy.Close();
               // proxy.Endpoint.Binding //System.ServiceModel.NetTcpBinding
}
using (ServiceReference1.MathServiceClient proxy = new ServiceReference1.MathServiceClient("WSHttpBinding_IMathService"))
{
                proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(new Uri("http://localhost:72/Service"));
                proxy.Endpoint.Binding = new WSHttpBinding();
                proxy.Endpoint.Contract.ContractType = typeof(IMathService);
                Console.WriteLine("HTTP: 2 + 2 is " + proxy.GetSum(2, 2));
                proxy.Close();
                //proxy.Endpoint.Binding //  System.ServiceModel.WSHttpBinding
            }
            Console.ReadLine();
        }
    }
In app.config file contains 2 bindings : NetTcpBinding and WsHttpBinding also. 
these configurations added whenever add reference(service reference) to client. both binding confiuguration added to client automatically.


as per client then consume the service.


How WCF service process the client requests?


yes, when client make a request the as per the request service will respone as per client Binding.


at client as per client  channel is prepared:
like 


yes, here our client is Net then client can utilize the netTcpBinding 


here Binding is : System.ServiceModel.NetTcpBinding for IMathService.


then Incase of BasicHttpBinding


bindig will be : System.ServiceModel.BasicHttpBinding for IMathService.  


so single service supported with multiple clients with multiple bindings.


as well single binding with BasicHttpBinding. also.


as Technical as wcf has a generic class is ClientBase<TChannel>   sdfasd    so as per client channel which will process the request.


for Binding of NetTcpBinding : ClientBase<NetTcpBinding>
for Binding of HttpBinding     : ClientBase<BasicHttpBinding>
for Binding of WsHttpBinding : ClientBase<WsHttpBinding> will be prepared and Process the request as per client
public abstract class ClientBase<TChannel> : ICommunicationObject, IDisposable where TChannel : class
    {

  Provides the base implementation used to create Windows Communication Foundation (WCF) client objects that can call services.
TChannel:
   The channel to be used to connect to the service.

}
so Generics Make make service as simple implementation.  

No comments:

Post a Comment