WCF Session is different with Asp.net session so don’t get confused. In WCF session means. to combine several message together into a single conversation. They are initiated from the calling application, in other word’s theyare client side and they can not store any kind of data inside.WCF Session are set by “SessionMode” Attribute.
You can have following types of modes
You can manage your session in WCF by using following method.
· Per Call
· Per Session
· Single Call You can define instancing mode with the help of instancecontext value ofServiceBehaviour attribute.[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
All above instantiations methods are not supported by basicHTTP binding. You make sure you have the change the binding as well. You can add binding information under the services tag in web.config.
Where IService is my contract and I have chosen wsHttpBinding type. While defining bindings you can define the address as well of your service. In WCF you can use two types of addressing
Now go to your contract page (Interface) and define your [OperationContract]
Each one has their own pros and cons, as per your requirement you can choose any of them. Before moving ahead let me explain the fundamentals of above mentioned ways. So let’s start with
Per Call – If your service is configured per call basis, each time a new instance of your service will be created. In other words for each call every time a new service instance will be created and after fulfilling the purpose will be destroyed.
In Service class you must have to define the instancing behavior of your service
Per call is helpful when you have scalability concern.
Per Session - Some time scalability is not your primary concern and you wanted to maintain the states between wcf calls, then you can implement per session mechanisms. In this one single instance will be created for a session. To implement per session instantiations you must have to change the value of instancecontextmode of ServiceBehaviour attribute
Single Call – Sometime you have the requirement to share some data globally through your wcf application, single call instantiations is the correct choice. Here scalability will not consider. To achieve single call instantiations you need to configure your wcf service as a “Single” instance mode
In this case once when client make the request to WCF, first time WCF instance will create and fulfill the user request, now it will remain into memory to serve the other request. If you notice here only single wcf service instance is used to server all client request.
Make sure before implementing wcf instantiation, you have added“SessionMode” Attribute in your WCF Service Contract as
You can have following types of modes
- Allowed: Specifies the contract supports the reliable sessions if the incoming connection supports them. Here Session can be allowed.
- Not Allowed: Contract never supports reliable sessions.
- Required: Specifies that the contract requires a reliable sessions at all time. Means session is mandatory.
You can manage your session in WCF by using following method.
· Per Call
· Per Session
· Single Call You can define instancing mode with the help of instancecontext value ofServiceBehaviour attribute.[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
All above instantiations methods are not supported by basicHTTP binding. You make sure you have the change the binding as well. You can add binding information under the services tag in web.config.
<system.serviceModel>
<services>
<service name="Service">
<endpoint binding="wsHttpBinding" contract="IService">endpoint>
<endpoint binding="wsHttpBinding" contract="IRsg">endpoint>
service>
<services>
<behaviors>
Where IService is my contract and I have chosen wsHttpBinding type. While defining bindings you can define the address as well of your service. In WCF you can use two types of addressing
- Absolute address where you can specify the fixed path and port number of your contract such as
<endpoint address="http://localhost:8080/calcservice" binding = "wsHttpBinding" contract="IMathCalc"/>
Relative address, where you are not fixing any address or port.
<endpoint binding="wsHttpBinding" contract="IService"><endpoint>
Now go to your contract page (Interface) and define your [OperationContract]
public interface IService{
[OperationContract]
string Method1();
Each one has their own pros and cons, as per your requirement you can choose any of them. Before moving ahead let me explain the fundamentals of above mentioned ways. So let’s start with
Per Call – If your service is configured per call basis, each time a new instance of your service will be created. In other words for each call every time a new service instance will be created and after fulfilling the purpose will be destroyed.
In Service class you must have to define the instancing behavior of your service
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service : IService
{
public string Method1()
{
return "Method1";
}
Per call is helpful when you have scalability concern.
Per Session - Some time scalability is not your primary concern and you wanted to maintain the states between wcf calls, then you can implement per session mechanisms. In this one single instance will be created for a session. To implement per session instantiations you must have to change the value of instancecontextmode of ServiceBehaviour attribute
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
Single Call – Sometime you have the requirement to share some data globally through your wcf application, single call instantiations is the correct choice. Here scalability will not consider. To achieve single call instantiations you need to configure your wcf service as a “Single” instance mode
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
In this case once when client make the request to WCF, first time WCF instance will create and fulfill the user request, now it will remain into memory to serve the other request. If you notice here only single wcf service instance is used to server all client request.
Make sure before implementing wcf instantiation, you have added“SessionMode” Attribute in your WCF Service Contract as
[ServiceContract (SessionMode = SessionMode.Required)]
//[ServiceContract]
public interface IService{
[OperationContract]
string Method1();
No comments:
Post a Comment