如何:创建要求会话的服务
会话在两个或更多个终结点之间创建一个共享状态,从而启用一些有用的功能,例如回调、多跳安全性以及客户端和服务实例之间的关联。有关 Windows Communication Foundation (WCF) 应用程序中的会话的更多信息,请参见使用会话。
指定协定需要其绑定来支持会话
创建至少包含一个操作的服务协定。有关如何创建服务协定的示例,请参见如何:定义 Windows Communication Foundation 服务协定。
修改声明协定的 System.ServiceModel.ServiceContractAttribute,将 System.ServiceModel.ServiceContractAttribute.SessionMode 属性设置为:
System.ServiceModel.SessionMode.Required(如果必须在会话中运行此协定)。
System.ServiceModel.SessionMode.Allowed(如果可以在会话中运行此协定)。
System.ServiceModel.SessionMode.NotAllowed(如果不得在会话中运行此协定)。
配置服务终结点以使用支持会话的绑定。下面的配置示例演示了支持 WS-ReliableMessaging 会话的 System.ServiceModel.WSDualHttpBinding 的用法。
<appSettings> <!-- use appSetting to configure base address provided by host --> <add key="baseAddress" value="https://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 绑定的要求。
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
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);
}
}
}
<appSettings>
<!-- use appSetting to configure base address provided by host -->
<add key="baseAddress" value="https://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.ServiceContractAttribute
System.ServiceModel.ServiceContractAttribute.SessionMode
System.ServiceModel.SessionMode