How to: Create a Service That Requires Sessions
Sessions create a shared state between two or more endpoints that enables useful features such as callbacks, multi-hop security, and associations between clients and service instances. For more information about sessions in Windows Communication Foundation (WCF) applications, see Using Sessions.
To specify that a contract require its binding to support sessions
Create a service contract with at least one operation. For an example of how to create a service contract, see How to: Define a Windows Communication Foundation Service Contract.
Modify the System.ServiceModel.ServiceContractAttribute that declares the contract by setting the System.ServiceModel.ServiceContractAttribute.SessionMode property to either:
System.ServiceModel.SessionMode.Required if this contract must be run within a session.
System.ServiceModel.SessionMode.Allowed if this contract can be run within a session.
System.ServiceModel.SessionMode.NotAllowed if this contract must not be run within a session.
Configure your service endpoint to use a binding that supports sessions. The following configuration example shows the use of the System.ServiceModel.WSDualHttpBinding, which supports a WS-ReliableMessaging session.
<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>
Example
The following example code shows how to specify a contract-level session requirement and use a configuration file to support that requirement with the System.ServiceModel.WSDualHttpBinding binding.
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>
See Also
Reference
System.ServiceModel.ServiceContractAttribute
System.ServiceModel.ServiceContractAttribute.SessionMode
System.ServiceModel.SessionMode
© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-08-07