SessionMode SessionMode SessionMode SessionMode Enum

定义

指定可用于指示支持协定需要或支持的可靠会话的值。Specifies the values available to indicate the support for reliable sessions that a contract requires or supports.

public enum class SessionMode
public enum SessionMode
type SessionMode = 
Public Enum SessionMode
继承

字段

Allowed Allowed Allowed Allowed 0

指定当传入绑定支持会话时,协定也支持会话。Specifies that the contract supports sessions if the incoming binding supports them.

NotAllowed NotAllowed NotAllowed NotAllowed 2

指定协定永不支持启动会话的绑定。Specifies that the contract never supports bindings that initiate sessions.

Required Required Required Required 1

指定协定需要会话绑定。Specifies that the contract requires a sessionful binding. 如果绑定并未配置为支持会话,则将引发异常。An exception is thrown if the binding is not configured to support session.

示例

下面的代码示例演示如何使用SessionMode的属性ServiceContractAttribute以指定IMyService服务协定需要支持会话状态的绑定。The following code example shows how to use the SessionMode property of the ServiceContractAttribute to specify that the IMyService service contract requires bindings that support session state.

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);
    }
  }
}


Imports System
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.
	  Thread.Sleep(2000)
	  Dim callerProxy As IHelloCallbackContract = OperationContext.Current.GetCallbackChannel(Of IHelloCallbackContract)()
            Dim response = "Service object " & Me.GetHashCode().ToString() & " received: " & greeting
	  Console.WriteLine("Sending back: " & response)
	  callerProxy.Reply(response)
	End Sub
  End Class
End Namespace

注解

SessionMode 枚举与 ServiceContractAttribute.SessionMode 属性一起使用,以要求、允许或禁止绑定使用连接至或支持服务协定的终结点之间的对话。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.

如果您的服务支持会话,则可以使用 ServiceBehaviorAttribute.InstanceContextMode 属性指定您的服务协定实现实例与通道会话之间的关系。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.

例如,如果SessionMode属性设置为允许和ServiceBehaviorAttribute.InstanceContextMode属性设置为System.ServiceModel.InstanceContextMode,客户端可以使用支持可靠会话重复调用同一个服务对象的绑定。For example, if the SessionMode property is set to Allowed and the ServiceBehaviorAttribute.InstanceContextMode property is set to System.ServiceModel.InstanceContextMode, a client can use a binding that supports reliable sessions to make repeated calls to the same service object.

因为会话是应用程序模型使用的通道级概念,因此协定中的 SessionMode 枚举和 ServiceBehaviorAttribute.InstanceContextMode 属性之间存在交互,它控制着通道和特定服务对象之间的关联。Because a session is a channel-level concept that the application model uses, there is an interaction between the SessionMode enumeration in a contract and the ServiceBehaviorAttribute.InstanceContextMode property, which controls the association between channels and specific service objects.

下表显示支持可靠会话或不支持可靠会话的传入通道的结果,假设已给定服务的 ServiceContractAttribute.SessionMode 属性和 ServiceBehaviorAttribute.InstanceContextMode 属性的组合值。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 the ServiceBehaviorAttribute.InstanceContextMode property.

InstanceContextMode 值InstanceContextMode Value 必需Required AllowedAllowed NotAllowedNotAllowed
PerCallPerCall -会话通道的行为:会话和System.ServiceModel.InstanceContext为每个调用。- Behavior with sessionful channel: A session and System.ServiceModel.InstanceContext for each call.
-无会话通道的行为:将引发异常。- Behavior with sessionless channel: An exception is thrown.
-会话通道的行为:会话和System.ServiceModel.InstanceContext为每个调用。- Behavior with sessionful channel: A session and System.ServiceModel.InstanceContext for each call.
-无会话通道的行为:System.ServiceModel.InstanceContext为每个调用。- Behavior with sessionless channel: An System.ServiceModel.InstanceContext for each call.
-会话通道的行为:将引发异常。- Behavior with sessionful channel: An exception is thrown.
-无会话通道的行为:System.ServiceModel.InstanceContext为每个调用。- Behavior with sessionless channel: An System.ServiceModel.InstanceContext for each call.
PerSessionPerSession -会话通道的行为:会话和System.ServiceModel.InstanceContext每个通道。- Behavior with sessionful channel: A session and System.ServiceModel.InstanceContext for each channel.
-无会话通道的行为:将引发异常。- Behavior with sessionless channel: An exception is thrown.
-会话通道的行为:会话和System.ServiceModel.InstanceContext每个通道。- Behavior with sessionful channel: A session and System.ServiceModel.InstanceContext for each channel.
-无会话通道的行为:System.ServiceModel.InstanceContext为每个调用。- Behavior with sessionless channel: An System.ServiceModel.InstanceContext for each call.
-会话通道的行为:将引发异常。- Behavior with sessionful channel: An exception is thrown.
-无会话通道的行为:System.ServiceModel.InstanceContext为每个调用。- Behavior with sessionless channel: An System.ServiceModel.InstanceContext for each call.
SingleSingle -会话通道的行为:一个会话和一个System.ServiceModel.InstanceContext所有调用。- Behavior with sessionful channel: One session and one System.ServiceModel.InstanceContext for all calls.
-无会话通道的行为:将引发异常。- Behavior with sessionless channel: An exception is thrown.
-会话通道的行为:会话和System.ServiceModel.InstanceContext每个创建的单一实例或用户指定的单一实例。- Behavior with sessionful channel: A session and System.ServiceModel.InstanceContext for each created singleton or for the user-specified singleton.
-无会话通道的行为:System.ServiceModel.InstanceContext每个创建的单一实例或用户指定的单一实例。- Behavior with sessionless channel: An System.ServiceModel.InstanceContext for each created singleton or for the user-specified singleton.
-会话通道的行为:将引发异常。- Behavior with sessionful channel: An exception is thrown.
-无会话通道的行为:System.ServiceModel.InstanceContext每个创建的单一实例或用户指定的单一实例。- Behavior with sessionless channel: An System.ServiceModel.InstanceContext for each created singleton or for the user-specified singleton.

适用于