ServiceContractAttribute.SessionMode 属性


获取或设置是否允许、不允许或要求会话。Gets or sets whether sessions are allowed, not allowed or required.

 property System::ServiceModel::SessionMode SessionMode { System::ServiceModel::SessionMode get(); void set(System::ServiceModel::SessionMode value); };
public System.ServiceModel.SessionMode SessionMode { get; set; }
member this.SessionMode : System.ServiceModel.SessionMode with get, set
Public Property SessionMode As SessionMode


指示是否允许、不允许或要求会话的 SessionModeA SessionMode that indicates whether sessions are allowed, not allowed, or required.


该值不是 SessionMode 值之一。The value is not one of the SessionMode values.


以下服务协定要求配置的绑定在与 SampleDuplexHello 服务实现交互时使用会话。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
    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);

  public class DuplexHello : IDuplexHello

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

      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.
      IHelloCallbackContract callerProxy
        = OperationContext.Current.GetCallbackChannel<IHelloCallbackContract>();
      string response = "Service object " + this.GetHashCode().ToString() + " received: " + greeting;
      Console.WriteLine("Sending back: " + response);

Imports System.Collections.Generic
Imports System.ServiceModel
Imports System.Threading

Namespace Microsoft.WCF.Documentation
    <ServiceContract(Name:="SampleDuplexHello", Namespace:="http://microsoft.wcf.documentation", _
                     CallbackContract:=GetType(IHelloCallbackContract), SessionMode:=SessionMode.Required)> _
    Public Interface IDuplexHello
        <OperationContract(IsOneWay:=True)> _
        Sub Hello(ByVal greeting As String)
    End Interface

  Public Interface IHelloCallbackContract
    <OperationContract(IsOneWay := True)> _
    Sub Reply(ByVal responseToGreeting As String)
  End Interface

  <ServiceBehaviorAttribute(InstanceContextMode:=InstanceContextMode.PerSession)> _
  Public Class DuplexHello
      Implements IDuplexHello

    Public Sub New()
      Console.WriteLine("Service object created: " & Me.GetHashCode().ToString())
    End Sub

    Protected Overrides Sub Finalize()
      Console.WriteLine("Service object destroyed: " & Me.GetHashCode().ToString())
    End Sub

    Public Sub Hello(ByVal greeting As String) Implements IDuplexHello.Hello
      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.
      Dim callerProxy As IHelloCallbackContract = OperationContext.Current.GetCallbackChannel(Of IHelloCallbackContract)()
            Dim response = "Service object " & Me.GetHashCode().ToString() & " received: " & greeting
      Console.WriteLine("Sending back: " & response)
    End Sub
  End Class
End Namespace


使用 SessionMode 属性来要求在终结点之间支持会话的绑定。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. 如果服务支持通道会话,则可以使用 InstanceContextMode 属性指定服务协定实现实例与通道会话之间的关系。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.

例如,如果 SessionMode 属性设置为 SessionMode.Required 并且 InstanceContextMode 属性设置为 PerSession,则客户端可以使用相同的连接对同一服务对象进行重复调用。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.


支持会话的信道支持服务实例与特定会话的默认关联。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 提供了四种类型的会话,您可以使用这些会话提供会话应用程序行为;每种类型的会话都提供特定于会话类型的其他行为。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. System.ServiceModel.Channels.SecurityBindingElement 支持安全会话,在这种情况下,通信的两端都已与加密和/或数字签名过程一致;所有消息都与该特定安全对话相关联。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. 例如,默认情况下,System.ServiceModel.WSHttpBinding包含对安全会话和可靠会话的支持,则默认情况下仅使用可对消息进行加密和数字签名的安全会话。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. System.ServiceModel.NetTcpBinding 支持 TCP/IP 连接公开的会话,以确保所有消息都由套接字级别的连接会话关联。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. 用于实现 WS-RELIABLEMESSAGING 规范的 System.ServiceModel.Channels.ReliableSessionBindingElement提供对可靠会话的支持,在这些会话中,消息按顺序传递并且只传递一次,从而使消息在会话期间跨多个节点传输。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. System.ServiceModel.NetMsmqBinding 提供 MSMQ 数据报会话。The System.ServiceModel.NetMsmqBinding provides MSMQ datagram sessions. 有关详细信息,请参阅WCF 中的队列For more information, see Queues in WCF.

请记住,设置 SessionMode 属性并不指定协定需要的会话类型,而只指定协定需要一个会话。Remember that setting the SessionMode property does not specify the type of session the contract requires, only that it requires one.