Windows Communication Foundation (WCF) - Instance Context Mode
Introduction
This article illustrates the use of the InstanceContextMode property used in the ServiceBehaviour attribute and how it can be so handy in the latest communication technology Windows Communication Foundation (WCF), introduced by Microsoft as a part of the .NET framework SDK 3.0 and later versions.
I will be creating a demo application which will make use of the various instance context modes available in WCF, along with the explanation.
Why WCF?
I thought of providing a little information about why Windows Communication Foundation (WCF) is required in the .NET framework when there are already communication technologies like web services, enterprise services and remoting. WCF is introduced by Microsoft to achieve a Single Unified API for.NET services, so that in the future if any one speaks about service communication in .NET it would be only Windows Communication Foundation (WCF). Even though web service has proved to be stable it has a lot of limitations like statelessness and it also uses heavy soap to communicate so it is relatively slow. If you take remoting into consideration it can be consumed only by a .NET client and it could either be stateless or can only afford a singleton state.
When you consider WCF, it has a lot of flexibility which would allow you to configure the WCF application as per your needs. It can use many multiple message formats like text, Soap, Binary, REST and MTOM and different bindings like basicHttpBinding, wsHttpBinding, netTcpBinding and dualHttpBindings.
WCF can also maintain session based instances, singleton and also be stateless. This is achieved using the InstanceContextMode property, which we will be discussing in this article using a demo application.
Three Instance Context Modes
As I have mentioned ealiern, InstanceContextMode is nothing but a property which is available in the System.ServiceModel. This library is the one which encloses almost all the important WCF members. InstanceContextMode property is mainly used in the ServiceBehaviour attribute in order to mention what kind of state maintenance your WCF application should provide. The ServiceBehaviour attribute is designed at decorating the implementation classes in WCF.
There is also an enumerator available in the Sytem.ServiceModel library named as InstanceContextMode and it can be used in order to set the property InstanceContextMode.
There are three instance context modes available. I've listed them below:
PerCall
When you provide the instance context mode property as PerCall, it is like instructing your implementation class that state doesn't have to be maintained, so all the calls would become stateless. Below is the sample C# code showing how the property has to be set.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
Since it is not going to maintain any states, you can use the basicHttpBinding for your WCF application.
Single
When you provide the instance context mode property as Single, it is like instructing your WCF implementation class to maintain a singleton state. What I meant by saying singleton would be clear for the readers who have exposure to the singleton design pattern in C# programming. A singleton class is a class for which at any point of time only one instance can be created and the same instance will be reused on subsequent requests."
Below is the sample C# code showing how the property has to be set.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
Since it is going to maintain a single instance state, you cannot use the basicHttpBinding, but you can use any of the other bindings like wsHttpBinding.
PerSession
When you provide the instance context mode property as PerSession, it is like instructing your implementation class state that it has to be maintained based on client sessions. This is one major advantage of using WCF which offers session based state maintenance which was never previously offered by any of Microsoft's communication technologies like Remoting or Web Services.
Below is the sample C# code showing how the property has to be set.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
No surprise that this also doesn't work with basicHttpBinding, but works with all the other available bindings.
Now let us move onto creating the Demo Application. Let's create a sample Banking Application which will explain the Instance Context Modes pretty noticeably.
This article illustrates the use of the InstanceContextMode property used in the ServiceBehaviour attribute and how it can be so handy in the latest communication technology Windows Communication Foundation (WCF), introduced by Microsoft as a part of the .NET framework SDK 3.0 and later versions.
I will be creating a demo application which will make use of the various instance context modes available in WCF, along with the explanation.
Why WCF?
I thought of providing a little information about why Windows Communication Foundation (WCF) is required in the .NET framework when there are already communication technologies like web services, enterprise services and remoting. WCF is introduced by Microsoft to achieve a Single Unified API for.NET services, so that in the future if any one speaks about service communication in .NET it would be only Windows Communication Foundation (WCF). Even though web service has proved to be stable it has a lot of limitations like statelessness and it also uses heavy soap to communicate so it is relatively slow. If you take remoting into consideration it can be consumed only by a .NET client and it could either be stateless or can only afford a singleton state.
When you consider WCF, it has a lot of flexibility which would allow you to configure the WCF application as per your needs. It can use many multiple message formats like text, Soap, Binary, REST and MTOM and different bindings like basicHttpBinding, wsHttpBinding, netTcpBinding and dualHttpBindings.
WCF can also maintain session based instances, singleton and also be stateless. This is achieved using the InstanceContextMode property, which we will be discussing in this article using a demo application.
Three Instance Context Modes
As I have mentioned ealiern, InstanceContextMode is nothing but a property which is available in the System.ServiceModel. This library is the one which encloses almost all the important WCF members. InstanceContextMode property is mainly used in the ServiceBehaviour attribute in order to mention what kind of state maintenance your WCF application should provide. The ServiceBehaviour attribute is designed at decorating the implementation classes in WCF.
There is also an enumerator available in the Sytem.ServiceModel library named as InstanceContextMode and it can be used in order to set the property InstanceContextMode.
There are three instance context modes available. I've listed them below:
PerCall
When you provide the instance context mode property as PerCall, it is like instructing your implementation class that state doesn't have to be maintained, so all the calls would become stateless. Below is the sample C# code showing how the property has to be set.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
Since it is not going to maintain any states, you can use the basicHttpBinding for your WCF application.
Single
When you provide the instance context mode property as Single, it is like instructing your WCF implementation class to maintain a singleton state. What I meant by saying singleton would be clear for the readers who have exposure to the singleton design pattern in C# programming. A singleton class is a class for which at any point of time only one instance can be created and the same instance will be reused on subsequent requests."
Below is the sample C# code showing how the property has to be set.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
Since it is going to maintain a single instance state, you cannot use the basicHttpBinding, but you can use any of the other bindings like wsHttpBinding.
PerSession
When you provide the instance context mode property as PerSession, it is like instructing your implementation class state that it has to be maintained based on client sessions. This is one major advantage of using WCF which offers session based state maintenance which was never previously offered by any of Microsoft's communication technologies like Remoting or Web Services.
Below is the sample C# code showing how the property has to be set.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
No surprise that this also doesn't work with basicHttpBinding, but works with all the other available bindings.
Now let us move onto creating the Demo Application. Let's create a sample Banking Application which will explain the Instance Context Modes pretty noticeably.
Comments