Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 38 - PerSession instance context mode in WCF

Suggested Videos
Part 35 - Sending large messages in WCF using MTOM
Part 36 - Instancing modes in WCF
Part 37 - PerCall instance context mode in WCF



In this video we will discuss, PerSession instance context mode in WCF with an example. This is continuation to Part 37. Please watch Part 37 before proceeding.

When the instance context mode for a wcf service is set to PerSession, a new instance of the service object is created for each new client session and maintained for the duration of that session. We will continue with the same exmple, that we started in Part 37.



To convert the SimpleService created in Part 37 from PerCall service to PerSession service, set the InstanceContextMode to PerSession in SimpleService.cs file as shown below.
// Set InstanceContextMode to PerSession
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class SimpleService : ISimpleService

Now run the client application and notice that, the value gets incremented each time.
persession instance context mode example

What are the implications of a PerCall WCF service?
1. State maintained between calls.
2. Greater memory consumption as service objects remain in memory until the client session times out. This negatively affects application scalability.
3. Concurrency is an issue for multi-threaded clients

Common interview question: How do you design a WCF service? Would you design it as a PerCall service or PerSession service?
This is a tricky question. We can't blindly say one is better over the other. 
1. PerCall and PerSession services have different strengths and weaknesses. 

2. If you prefer using object oriented programming style, then PerSession is your choice. On the other hand if you prefer SOA (Service Oriented Arhcitecture) style, then PerCall is your choice.

3. In general, all things being equal, the trade-off is performance v/s scalability. PerSession services perform better because the service object does not have to be instantiated on subsequent requests. PerCall services scale better because the service objects are destroyed immediately after the method call returns.

So the decision really depends on the application architecture, performance & scalability needs.

wcf tutorial

No comments:

Post a Comment

It would be great if you can help share these free resources