SessionMode 列舉

定義

指定可用的值以指出對合約所要求或支援的可靠工作階段所提供的支援。

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

欄位

Allowed 0

指定合約是否支援工作階段 (如果傳入繫結支援工作階段的話)。

NotAllowed 2

指定合約永不支援會產生工作階段的繫結。

Required 1

指定合約需要工作階段繫結。 如果未設定繫結來支援工作階段,就會擲回例外狀況。

範例

下列程式碼範例示範如何使用 SessionMode 的 屬性來指定 IMyService 服務合約需要支援會話狀態的 ServiceContractAttribute 系結。

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.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 屬性,以要求、允許或禁止系結在連線至或支援服務合約的端點之間使用會話。 工作階段是一種將兩個 (或更多) 端點間所交換的一組訊息予以關聯的方式。 如需會話的詳細資訊,請參閱 使用會話

如果您的服務支援會話,您可以使用 ServiceBehaviorAttribute.InstanceContextMode 屬性來指定服務合約實作實例與通道會話之間的關聯性。

例如,如果 ServiceContractAttribute.SessionMode 屬性設定 Allowed 為 ,而 ServiceBehaviorAttribute.InstanceContextMode 屬性設定 InstanceContextMode.PerSession 為 ,則用戶端可以使用支援可靠會話的系結,對相同的服務物件進行重複呼叫。

因為會話是應用程式模型使用的通道層級概念,所以合約中的列舉與 屬性之間 SessionMode 有互動,可控制通道與 ServiceBehaviorAttribute.InstanceContextMode 特定服務物件之間的關聯。

下表顯示傳入通道的結果:支援可靠的會話,或不支援可靠的會話,因為服務結合屬性和 ServiceBehaviorAttribute.InstanceContextMode 屬性的值 ServiceContractAttribute.SessionMode

InstanceContextMode 值 必要 允許 NotAllowed
PerCall - 具有會話通道的行為:會話和 System.ServiceModel.InstanceContext 每個呼叫。
- 無會話通道的行為:擲回例外狀況。
- 具有會話通道的行為:會話和 System.ServiceModel.InstanceContext 每個呼叫。
- 具有無會話通道的行為: System.ServiceModel.InstanceContext 每個呼叫的 。
- 具有會話通道的行為:擲回例外狀況。
- 具有無會話通道的行為: System.ServiceModel.InstanceContext 每個呼叫的 。
PerSession - 具有會話通道的行為:會話和 System.ServiceModel.InstanceContext 每個通道的行為。
- 無會話通道的行為:擲回例外狀況。
- 具有會話通道的行為:會話和 System.ServiceModel.InstanceContext 每個通道的行為。
- 具有無會話通道的行為: System.ServiceModel.InstanceContext 每個呼叫的 。
- 具有會話通道的行為:擲回例外狀況。
- 具有無會話通道的行為: System.ServiceModel.InstanceContext 每個呼叫的 。
Single - 具有會話通道的行為:一個會話,一個 System.ServiceModel.InstanceContext 用於所有呼叫。
- 無會話通道的行為:擲回例外狀況。
- 具有會話通道的行為:會話,以及 System.ServiceModel.InstanceContext 針對每個建立的單一或使用者指定的單一。
- 具有無會話通道的行為: System.ServiceModel.InstanceContext 針對每個建立的單一或使用者指定的單一。
- 具有會話通道的行為:擲回例外狀況。
- 具有無會話通道的行為: System.ServiceModel.InstanceContext 針對每個建立的單一或使用者指定的單一。

適用於