Редагувати

Поділитися через


SessionMode Enum

Definition

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
Inheritance
SessionMode

Fields

Name Value Description
Allowed 0

Specifies that the contract supports sessions if the incoming binding supports them.

Required 1

Specifies that the contract requires a sessionful binding. An exception is thrown if the binding is not configured to support session.

NotAllowed 2

Specifies that the contract never supports bindings that initiate sessions.

Examples

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.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

Remarks

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.

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.

For example, if the ServiceContractAttribute.SessionMode property is set to Allowed and the ServiceBehaviorAttribute.InstanceContextMode property is set to InstanceContextMode.PerSession, a client can use a binding that supports reliable sessions to make repeated calls to the same service object.

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.

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 Value Required Allowed NotAllowed
PerCall - Behavior with sessionful channel: A session and System.ServiceModel.InstanceContext for each call.
- Behavior with sessionless channel: An exception is thrown.
- Behavior with sessionful channel: A session and System.ServiceModel.InstanceContext for each call.
- Behavior with sessionless channel: An System.ServiceModel.InstanceContext for each call.
- Behavior with sessionful channel: An exception is thrown.
- Behavior with sessionless channel: An System.ServiceModel.InstanceContext for each call.
PerSession - Behavior with sessionful channel: A session and System.ServiceModel.InstanceContext for each channel.
- Behavior with sessionless channel: An exception is thrown.
- Behavior with sessionful channel: A session and System.ServiceModel.InstanceContext for each channel.
- Behavior with sessionless channel: An System.ServiceModel.InstanceContext for each call.
- Behavior with sessionful channel: An exception is thrown.
- Behavior with sessionless channel: An System.ServiceModel.InstanceContext for each call.
Single - Behavior with sessionful channel: One session and one System.ServiceModel.InstanceContext for all calls.
- Behavior with sessionless channel: An exception is thrown.
- Behavior with sessionful channel: A session and System.ServiceModel.InstanceContext for each created singleton or for the user-specified singleton.
- 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.
- Behavior with sessionless channel: An System.ServiceModel.InstanceContext for each created singleton or for the user-specified singleton.

Applies to