ServiceContractAttribute.SessionMode 屬性

定義

取得或設定是否允許工作階段、不允許工作階段,或需要工作階段。

public:
 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

屬性值

SessionMode,表示是否允許工作階段、不允許工作階段,或需要工作階段。

例外狀況

該值不是其中一個 SessionMode 值。

範例

下列服務合約需要設定系結在與 SampleDuplexHello 服務實作互動時使用會話。

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使用 屬性需要支援端點之間會話的系結。 工作階段是一種將兩個 (或更多) 端點間所交換的一組訊息予以關聯的方式。 如果您的服務支援通道會話,您可以使用 InstanceContextMode 屬性來指定服務合約實作與通道會話之間的關聯性。 若繫結不支援工作階段,就會擲回例外狀況。

例如,如果 SessionMode 屬性設定 SessionMode.Required 為 ,而 InstanceContextMode 屬性設定 PerSession 為 ,則用戶端可以使用相同的連接對相同服務物件進行重複呼叫。

如需會話和服務實例的詳細資訊,請參閱使用會話和服務實例、實例和並行。

注意

支援工作階段的通道,可支援服務執行個體與特定工作階段之間的預設關聯。 但是,除了工作階段的執行個體控制外,不同工作階段實作會支援不同功能。 WCF 提供四種類型的會話,可讓您用來提供會話應用程式行為;每個會話類型都會提供它所指定之會話類型的特定其他行為。

  1. System.ServiceModel.Channels.SecurityBindingElement支援安全性會話,其中兩端通訊都已同意加密和/或數位簽章程式;所有訊息都會與該特定安全交談相互關聯。 如需詳細資訊,請參閱 保護服務。 例如, System.ServiceModel.WSHttpBinding 包含安全性會話和可靠會話的支援,預設只會使用加密和數位簽署訊息的安全會話。

  2. System.ServiceModel.NetTcpBinding支援 TCP/IP 連線公開的會話,以確保所有訊息都與通訊端層級的連線會話相互關聯。

  3. 實作WS-ReliableMessaging規格的 , System.ServiceModel.Channels.ReliableSessionBindingElement 可支援可依序傳遞訊息且完全一次的可靠會話,即使訊息在交談期間跨多個節點移動,仍能信賴度。 如需詳細資訊,請參閱 Reliable Sessions

  4. 提供 System.ServiceModel.NetMsmqBinding MSMQ 資料包會話。 如需詳細資訊,請參閱 WCF 中的佇列

請記住,設定 SessionMode 屬性不會指定合約所需的會話類型,只需要一個會話。

適用於