Reputation: 62101
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?
Upvotes: 6
Views: 5987
Reputation: 2345
[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.
Upvotes: 9
Reputation: 43
Set you're timeout values and create a PingService() method that fires just before the timeout elapses, this will reset the Send and Receive timeouts.
if you want to catch the timeout simply attempt a callback, if the attempt takes longer than the binding SendTimeout value (default is 1 min i think), then you'll throw a time-out error and you can process the disconnect.
There's no way to gracefully catch a disconnected user (browser crashes, power goes out, etc., etc., etc.)
Upvotes: -1
Reputation: 65411
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.
Upvotes: 4