세션은 콜백, 다중 홉 보안 및 클라이언트와 서비스 인스턴스 간의 연결과 같은 유용한 기능을 가능하게 하는 둘 이상의 엔드포인트 간에 공유 상태를 만듭니다. WCF(Windows Communication Foundation) 애플리케이션의 세션에 대한 자세한 내용은 세션 사용을 참조하세요.
계약이 세션을 지원하도록 바인딩이 필요하다는 것을 지정하려면
하나 이상의 오퍼레이션을 사용하여 서비스 계약을 만듭니다. 서비스 계약을 만드는 방법의 예는 방법: 서비스 계약 정의를 참조하세요.
System.ServiceModel.ServiceContractAttribute 속성을 다음 중 하나로 설정하여 계약을 선언하는 ServiceContractAttribute.SessionMode 항목을 수정합니다.
SessionMode.Required 이 계약이 세션 내에서 실행되어야 하는 경우.
SessionMode.Allowed 이 계약을 세션 내에서 실행할 수 있으면 입니다.
SessionMode.NotAllowed 이 계약은 세션 내에서 실행하지 않아야 합니다.
세션을 지원하는 바인딩을 사용하도록 서비스 엔드포인트를 구성합니다. 다음 구성 예제는 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>