How to Configure Sessions in WCF?
To configure sessions in WCF, one should know the following three elements:
Binding
– Because all bindings do not support sessions. Only WS-*, NetTcpBinding andNetNamedPipeBinding
have session support so selection of appropriate binding is necessary.SessionMode
– This service contract specifies service’s possible expectation for session from incoming client request. It has three self describing values:Allowed
– Service can accept sessionful clients as well as sessionless.Required
– Service will entertain only those clients who have session, sessionless client cannot connect to service.NotAllowed
– Service can interact only with sessionless clients. Just opposite toRequired
.
InstanceContextMode
– This is a service behavior that controls instantiation of actual service class. It has the following values:PerCall
– Each request made to server will be served by a new instance of service class.PerSession
– A Session will have its dedicated service instance and all requests pertaining to the session will be served by that instance only. All sessions will have their individual dedicated service instances.Single
–All requests to the service will be served by a single unique instance of service class[ServiceContract(SessionMode = SessionMode.Allowed)] public interface Iservice { //some code… }
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class serviceclass : Iservice { // some code… }
No comments:
Post a Comment