Thursday, December 10, 2015

What is WCF service behavior?

I will explain about Service behaviors and how to implement in WCF.
Service behaviors is used to increase through put, scalability and to improve performance of service applications.
There is different service behaviour that determines the performance of service.

1.       Concurrency:  
By default WCF service handle only single request at a time and all other request are queued up and processed one by one.
Client can send any request but WCF service should be able to handle all requests at a time without deadlock or fail.

There are three types of Concurrency modes.

·         Single(Default): Concurrency mode single will allows only one request at a time and other request will be put in queue and handle one by one. Only single instance of service will be created.

Ex:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]
public class Service : IService
{

}

·         Multiple:  This mode will allow to handle multiple request parallel. Multi threads are create to each request and handle all together.
It will improve perfomance of WCF service.

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service : IService
{

                 }
·         Re-entrant:  Some times one service calls another service internally. In that case when client make request then request is assign a thread lock and it will not be released until both services are completed.
                                So if this mode is enable client request has to finish the first service call and release the lock.
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class Service : IService
{

2.       Instance Context Mode:
      Instance mode decide how WCF service instance will be created and what will  be life time of object.
Whenever client request a call instance of service is created.

·         Per Call:  Every time WCF service instance will be created when client request a service.  This will work with all type of service binding.

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall)
)]
public class Service : IService
{

                 }
·         Per Session(Default): Only one service instance is created for a client request and instance will be dispose call request end. So instance per client will be created.
                                                 This is dfault value for instancing service. It works with all bindings except basichttpbindings.
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSessionl)
)]
public class Service : IService
{

                 }
·         Single:  Single service instance is created for all client. It will be disposed when service is down. It is singleton instance of service.

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)
)]
public class Service : IService
{

                 }

No comments:

Post a Comment

Write a program to reverse a string? using System; namespace ConsoleApp1 {     class Program     {         static void Main(string[] args)  ...