WCF

WCF Examples

WCF

WCF Projects

WCF Project

Per-Session Services
Previous Home Next

Per service endpoint per proxy is the client session.

If the client creates another proxy to the same or a different endpoint then that second proxy will be associated with a new instance and session.

Configuring Private Sessions:

A session has three elements

  1. behavior,
  2. binding,
  3. contract.

 

The behavior part is used  for that WCF will keep the service instance alive throughout the session and to direct the client messages to it.

This local behavior is created by setting the InstanceContextMode property of the ServiceBehavior attribute to InstanceContextMode.PerSession:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
class MyService : IMyContract
{...}

Since InstanceContextMode.PerSession is the default value of the InstanceContextMode property, these definitions are equivalent:

class MyService : IMyContract
{...}
[ServiceBehavior]
class MyService : IMyContract
{...}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
class MyService : IMyContract
{...}
SessionMode.Allowed

SessionMode.Allowed is the default value of the property, so these definitions are equivalent:

[ServiceContract]
interface IMyContract
{...}

[ServiceContract(SessionMode = SessionMode.Allowed)]
interface IMyContract
{...}

Example Per-session service and client

///////////////////////// Service code /////////////////////
[ServiceContract(SessionMode = SessionMode.Required)]
interface IMyContract
{
[OperationContract]
void operation( );
}
class MyService : IMyContract,IDisposable
{
int flag= 0;
MyService( )
{
Trace.WriteLine("MyService.MyService( )");
}
public void operation( )
{
flag++;
Trace.WriteLine("Counter = " + flag);
}
public void Dispose( )
{
Trace.WriteLine("MyService.Dispose( )");
}
}
///////////////////////// Client code /////////////////////
MyContractClient proxy = new MyContractClient( );
proxy.operation( );
proxy.operation( );
proxy.Close( );
//Output
MyService.MyService( )
Counter = 1
Counter = 2
MyService.Dispose( )
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: