I've implemented a WCF service as such:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single, UseSynchronizationContext=false)]
public sealed class SynchronizationService : ISynchronizationService, IDisposable
{
private MemoryStream objectStream;
...
}
ISyncrhonizationService has [ServiceContract(SessionMode = SessionMode.Required)].
If a client communicates correctly and finally calls a method with [IsTerminating=true], then I can dispose of the objectStream variable nicely. If for some reason the communication between the service and the client gets disrupted, I'd like to clean up the objectStream variable from the service side myself, which is why I want to implement the IDisposable interface. But looking at the MSDN documentation (http://msdn.microsoft.com/en-us/library/system.idisposable.aspx) I'm a bit sceptical about this, because as per the documentation:
// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
Because it's the runtime that will call the Dispose method then disposing = false. Thus I should not access objectStream, because it is a managed object. In this case the SyncrhonizationService finalizer provided by the runtime should clean up of objectStream itself and I don't need to implement IDisposable at all. Yet some forum posts suggest that the SyncrhonizationService instance will instead be reused and not disposed/finalized.
So my questions are:
1) What actually happens to the SyncrhonizationService instance during exceptions/faults/timeouts/etc? Will it be disposed or simply reused at a later time?
2) Do I need to implement IDisposable and do I dispose of managed objetcs inside the Dispose() method?
3) Is there perhaps a better alternative to IDisposable, for instance something like a channel_Faulted event? If I do dispose of my managed objects in such an event, how and where will I remove the event handler (I could do it in the event handler, but what if another type of exception/fault occurs)?