Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF: How to find out when a session is ending?

Tags:

session

wcf

I have a WCF application that is using sessions.

Is there any central event to get thrown when a session ends? How can I find out when a session is ending WITHOUT (!) calling a method (network disconnect, client crashing - so no "logout" method call)?

The server is hosted as:

[ServiceBehavior(
    InstanceContextMode = InstanceContextMode.PerSession,
    ConcurrencyMode = ConcurrencyMode.Reentrant,
    UseSynchronizationContext = false,
    IncludeExceptionDetailInFaults = true
)]

Basically because it is using a callback interface.

Now, I basically need to decoubple the instance created from the backend store when the session terminates ;)

Any ideas?

like image 834
TomTom Avatar asked May 07 '10 11:05

TomTom


2 Answers

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
class MyService : IService
{
    public MyService()
    {
        // Session opened here
        OperationContext.Current.InstanceContext.Closed += InstanceContext_Closed;
        // get the callback instance here if needed
        // OperationContext.Current.GetCallbackChannel<IServiceCallback>()
    }

    private void InstanceContext_Closed(object sender, EventArgs e)
    {
        // Session closed here
    }
}

The code won't work for Single/PerCall InstanceContextMode.

like image 113
HelloSam Avatar answered Oct 13 '22 20:10

HelloSam


There is a good description of instance context here

Instance context derives from CommunicationObject

Communication object has a closing event.

I have not done this myself, but in theory it should be possible to use this event to decouple from the backend store when the session closes, by hooking into this event.

like image 27
Shiraz Bhaiji Avatar answered Oct 13 '22 18:10

Shiraz Bhaiji