Understanding WCF Session

  Member name Description
  Allowed Specifies that the contract supports sessions if the incoming binding supports them.
  Required Specifies that the contract requires a sessionful binding. An exception is thrown if the binding is not configured to support session.
  NotAllowed Specifies that the contract never supports bindings that initiate sessions.

Explaination:

Firstly. When I do some experiment about this topic. There come up a question to my brain . that is What is WCF Session ? In my opion.  I think A different channel means a different session.

If you specify the ServiceContractAttribute.SessionMode is required . you can not use the sessionless bindings like basicHttpBinding.

Because all bindings do not support sessions. Only WS-*, NetTcpBinding and NetNamedPipeBinding have session support so selection of appropriate binding is necessary.by the way . Wcf allow you define the combination of not allowed session mode with WSHttpBinding which support session .

so . if not . you got the exception message below .

Understanding WCF Session_第1张图片

 

Use the SessionMode enumeration with the ServiceContractAttribute.SessionMode property to require, allow, or prohibit bindings to use sessions between endpoints that connect to or support the service contract. A session is a way of correlating a set of messages exchanged between two or more endpoints. For more information about sessions, see Using Sessions.

If your service supports sessions, you can then use the ServiceBehaviorAttribute.InstanceContextMode property to specify the relationship between instances of your service contract implementation and the channel session.

For example, if the SessionMode property is set to SessionMode.Allowed and theServiceBehaviorAttribute.InstanceContextMode property is set to InstanceContextMode.PerSession, a client can use a binding that supports reliable sessions to make repeated calls to the same service object.

Because a session is a channel-level concept that the application model uses, there is an interaction between theSessionMode enumeration in a contract and the ServiceBehaviorAttribute.InstanceContextMode property, which controls the association between channels and specific service objects.

The following table shows the result of an incoming channel either supporting reliable sessions or not supporting reliable sessions given a service's combination of the values of the ServiceContractAttribute.SessionMode property and theServiceBehaviorAttribute.InstanceContextMode property.

 

InstanceContextMode Value

Required

Allowed

NotAllowed

PerCall

PerSession

Single

  • Behavior with sessionful channel: An exception is thrown.

  • Behavior with sessionless channel: AnSystem.ServiceModel.InstanceContextfor each created singleton or for the user-specified singleton.

Note:

Whether a channel is sessionful or not depends on the binding which is used to create the channel. The SessionMode property is used for validation, during runtime, that the endpoint for that contract is using an appropriate binding.

For the three values of session mode:

  • Required: that means that the binding used in the endpoint for the contract must support sessions. If a binding does not support session, then the client will throw when being used for the first time, or the service host will throw during Open
  • NotAllowed: the opposite: if the binding used in the endpoint does support sessions, then the runtime will throw (while opening the service or the client)
  • Allowed: the default value, the contract can be used for all bindings regardless of their support for sessions.

The remarks section of the MSDN page for the property has more information about this property.

你可能感兴趣的:(session)