ServiceBehavior- Specifies the internal execution behavior of a service contract implementation.
ServiceBehaviour attribute is used to specify the InstanceContextMode for the WCF Service class.
(This can be used to maintained a state of the service or a client too)
There are three instance Context Mode in the WFC ::
PerSession : This is used to create a new instance for a service and the same instance is used for all method for a particular client. (eg: State can be maintained per session by declaring a variable)
A note on SessionMode
ServiceBehaviour attribute is used to specify the InstanceContextMode for the WCF Service class.
(This can be used to maintained a state of the service or a client too)
There are three instance Context Mode in the WFC ::
PerSession : This is used to create a new instance for a service and the same instance is used for all method for a particular client. (eg: State can be maintained per session by declaring a variable)
PerCall : This is used to create a new instance for every call from the client whether same client or different. (eg: No state can be maintained as every time a new instance of the service is created)
Single : This is used to create only one instance of the service and the same instance is used for all the client request. (eg: Global state can be maintained but this will be applicable for all clients)
Instance mode can be configured using ServiceBehavior attribute. This can be specified at implementing the service contract as shown below.
[ServiceContract]
public interface IService1
{
[OperationContract(SessionMode.Allowed)]
int Increment();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Percall)]
//PerSession, Single
public class Service1 : IService1
{
private int intCounter;
public int Increment()
{
intCounter++;
return intCounter;
}
}
A note on SessionMode
In the service contract we can also specify a property for
configuring
SessionMode
.
This property specifies how the sessions will be created on client
requests. We can use this property to take control over the session creation
for our WCF services.
There are three possible values for this property.
·
Allowed
·
NotAllowed
·
Required
The
Allowed
option specifies
that this service will accept request from both sessionful and sessionless
clients. Having sessions is not mandatory but if the clients want to work in
sessionful manner, it is allowed. This mode is the default mode for WCF services.
The
NotAllowed
option specifies that the service will not work in sessionful
mode i.e. it will only accept requests from the sessionless/stateless clients.
In case any sessionful/stateful client tries to make a request, an exception
will be thrown.
The R
equired
option
states that the service will only work in sessionful environment i.e. only
sessionful/stateful clients can request the service operations. In case of
stateless clients making any requests, an exception will be thrown.
These modes are important because the resulting behavior of the
the WCF service will depend on the options selected for
InstanceContextMode
in ServiceBehavior
and SessionMode
in ServiceContract
. So
these options must be set carefully with the desired behavior in mind.
0 comments:
Post a Comment