Thursday, 6 September 2012

WCF Service behaviour default binding settings


Yes, based on binding(basicHttpBinding, wsHttpBinding,netTcpBinding…ect)default values are changed
Default Service behaviours
EX:
We can customize the behaviour of service(called as servicebehaviour). Using InstanceContextMode, ConcurrencyMode settings.

These default values are changed as per bindings.

Demo:
namespace WCFConcurrencyDemo
{
    [ServiceContract]
    interface IConcurrenyDemo
    {
        [OperationContract(IsOneWay = true)]
        void Call(string ClientName);
    }
   //[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession,ConcurrencyMode=ConcurrencyMode.Single)]
    [ServiceBehavior]
    class ConcurrencyDemo : IConcurrenyDemo
    {

        public int i;
        public void Call(string ClientName)
        {
            i++;
            Console.WriteLine("Client name :" + ClientName + " Instance:" +
         i.ToString() + " Thread:" + Thread.CurrentThread.ManagedThreadId.ToString() +" Time:" + DateTime.Now.ToString() + "\n\n");
            // Wait for 5 seconds
            Thread.Sleep(5000);
        }

      
    }

}


Hosting the Service:
namespace WCFConcurrencyDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri httpUrl = new Uri("http://localhost:8010/MyService/HelloWorld");
            //Create ServiceHost
            ServiceHost host = new ServiceHost(typeof(ConcurrencyDemo), httpUrl);
            //Add a service endpoint
host.AddServiceEndpoint(typeof(IConcurrenyDemo), new BasicHttpBinding(), "");   // In case of BasicHttpBinding

 host.AddServiceEndpoint(typeof(IConcurrenyDemo), new WSHttpBinding()"");    
// In case of WSHttpBinding
            //Enable metadata exchange
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            host.Description.Behaviors.Add(smb);
            //Start the Service
            host.Open();
            Console.WriteLine("Service is host at " + DateTime.Now.ToString());
            Console.WriteLine("Host is running... Press <Enter> key to stop");
            Console.ReadLine();
        }
    }
}

Client Consume the service:





basicHttpBinding:
In case of basicHttpBinding per every client call create a new instance of service object.and new threads are created.


WsHttpBinding:
In case of WsHttpBinding per every client call create single instance at first request then 2nd request onwords utilizes the old service instance.then single thread is created and serve the all the requests.



Finally

For EVERY REQUEST

basicHttpbinding: new instance,new thread------> Percall

wshttpbinding: single instancem single thread--->PerSession.

Demo 2:my another article is BasicHttpBinding(Percall),WsHttpBinding(Persession)


BasicHttpBinding concurrency default is Concurrency is PerCall
default basicHttpBinding:
1111111111

this is wsHttpBinding concurrency default is Concurrency is PerSession
default wsHttpBinding:
12345678910





No comments:

Post a Comment