HOW TO:建立需要工作階段的服務
工作階段會在兩個或更多的端點之間建立共用狀態以啟用諸如回呼、多重躍點安全性之類的有用功能,並在用戶端與服務執行個體之間建立關聯。如需詳細資訊 Windows Communication Foundation (WCF) 應用程式中的工作階段詳細資訊,請參閱使用工作階段。
指定合約需要自身繫結來支援工作階段
建立其中至少包含一個作業的服務合約。如需如何建立服務合約的範例,請參閱 HOW TO:定義 Windows Communication Foundation 服務合約。
將 System.ServiceModel.ServiceContractAttribute.SessionMode 屬性設為下列其中一項,以修改宣告合約的 System.ServiceModel.ServiceContractAttribute:
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