如何:创建要求会话的服务

会话在两个或更多终结点之间创建一个共享状态,从而启用一些有用的功能,例如回调、多跳安全性以及客户端和服务实例之间的关联。 有关 Windows Communication Foundation (WCF) 应用程序中的会话的详细信息,请参阅使用会话

指定协定需要其绑定来支持会话

  1. 创建至少包含一个操作的服务协定。 有关如何创建服务协定的示例,请参阅如何:定义服务协定

  2. 修改声明协定的 System.ServiceModel.ServiceContractAttribute,将 ServiceContractAttribute.SessionMode 属性设置为:

  3. 配置服务终结点以使用支持会话的绑定。 下面的配置示例演示了支持 WSSystem.ServiceModel.WSDualHttpBindingReliableMessaging 会话的 - 的用法。

    <appSettings>
      <!-- use appSetting to configure base address provided by host -->
      <add key="baseAddress" value="http://localhost:8080/ServiceMetadata" />
    </appSettings>
    <system.serviceModel>
      <services>
        <service 
          name="Microsoft.WCF.Documentation.DuplexHello"
          behaviorConfiguration="mex"
        >
          <endpoint
            address="/DuplexService"
            binding="wsDualHttpBinding"
            contract="Microsoft.WCF.Documentation.IDuplexHello"
           />
          <endpoint
            address=""
            binding="mexHttpBinding"
            contract="IMetadataExchange"
          />
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="mex" >
            <serviceMetadata httpGetEnabled="true" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
    

示例

下面的示例代码演示如何指定协定级别会话需求并使用配置文件来支持 System.ServiceModel.WSDualHttpBinding 绑定的需求。

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
<appSettings>
  <!-- use appSetting to configure base address provided by host -->
  <add key="baseAddress" value="http://localhost:8080/ServiceMetadata" />
</appSettings>
<system.serviceModel>
  <services>
    <service 
      name="Microsoft.WCF.Documentation.DuplexHello"
      behaviorConfiguration="mex"
    >
      <endpoint
        address="/DuplexService"
        binding="wsDualHttpBinding"
        contract="Microsoft.WCF.Documentation.IDuplexHello"
       />
      <endpoint
        address=""
        binding="mexHttpBinding"
        contract="IMetadataExchange"
      />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="mex" >
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

请参阅