Export (0) Print
Expand All

ServiceContractAttribute.SessionMode Property

Gets or sets whether sessions are allowed, not allowed or required.

Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public SessionMode SessionMode { get; set; }

Property Value

Type: System.ServiceModel.SessionMode
A SessionMode that indicates whether sessions are allowed, not allowed, or required.

ExceptionCondition
ArgumentOutOfRangeException

The value is not one of the System.ServiceModel.SessionMode values.

Use the SessionMode property to require bindings that support sessions between endpoints. A session is a way of correlating a set of messages exchanged between two or more endpoints. If your service supports channel sessions, you can then use the InstanceContextMode property to specify the relationship between instances your service contract implementation and the channel session. If a binding does not support sessions, an exception is thrown.

For example, if the SessionMode property is set to SessionMode.Required and the InstanceContextMode property is set to PerSession, clients can use the same connection to make repeated calls to the same service object.

For more information about sessions and service instances, see Using Sessions and Sessions, Instancing, and Concurrency.

NoteNote

A channel that supports sessions supports the default association of a service instance with a particular session. However, different session implementations support different features in addition to session-based instancing control. WCF provides four types of sessions that you can use to provide sessionful application behavior; each type of session provides additional behavior specific to the type of session it is.

  1. The System.ServiceModel.Channels.SecurityBindingElement supports security sessions, in which both ends of communication have agreed upon an encryption and/or digital signature process; all messages are correlated with that specific secure conversation. For more information, see Securing Services. For example, the System.ServiceModel.WSHttpBinding, which contains support for both security sessions and reliable sessions, by default uses only a secure session which encrypts and digitally signs messages.

  2. The System.ServiceModel.NetTcpBinding supports the sessions exposed by the TCP/IP connections to ensure that all messages are correlated by the connection session at the socket level.

  3. The System.ServiceModel.Channels.ReliableSessionBindingElement, which implements the WS-ReliableMessaging specification, provides support for reliable sessions in which messages are delivered in order and exactly once, enabling confidence even when messages travel across multiple nodes during the conversation. For more information, see Reliable Sessions.

  4. The System.ServiceModel.NetMsmqBinding provides MSMQ datagram sessions. For more information, see Queues in Windows Communication Foundation.

Remember that setting the SessionMode property does not specify the type of session the contract requires, only that it requires one.

The following service contract requires that configured bindings use sessions when interacting with SampleDuplexHello service implementations.

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(
    Name = "SampleDuplexHello",
    Namespace = "http://microsoft.wcf.documentation",
    CallbackContract = typeof(IHelloCallbackContract),
    SessionMode = SessionMode.Required
  )]
  public interface IDuplexHello
  {
    [OperationContract(IsOneWay = true)]
    void Hello(string greeting);
  }

  public interface IHelloCallbackContract
  {
    [OperationContract(IsOneWay = true)]
    void Reply(string responseToGreeting);
  }

  [ServiceBehaviorAttribute(InstanceContextMode=InstanceContextMode.PerSession)]
  public class DuplexHello : IDuplexHello
  {

    public DuplexHello()
    {
      Console.WriteLine("Service object created: " + this.GetHashCode().ToString());
    }

    ~DuplexHello()
    {
      Console.WriteLine("Service object destroyed: " + this.GetHashCode().ToString());
    }

    public void Hello(string greeting)
    {
      Console.WriteLine("Caller sent: " + greeting);
      Console.WriteLine("Session ID: " + OperationContext.Current.SessionId);
      Console.WriteLine("Waiting two seconds before returning call.");
      // Put a slight delay to demonstrate asynchronous behavior on client.
      Thread.Sleep(2000);
      IHelloCallbackContract callerProxy
        = OperationContext.Current.GetCallbackChannel<IHelloCallbackContract>();
      string response = "Service object " + this.GetHashCode().ToString() + " received: " + greeting;
      Console.WriteLine("Sending back: " + response);
      callerProxy.Reply(response);
    }
  }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft