I have defined the following DataContract
which implements IDisposable
:
[DataContract]
public class RegularFileMetadata : FileMetadataBase, IDisposable
{
bool _Disposed = false; //note this!
//...
protected virtual void Dispose(bool disposing)
{
if (!_Disposed)
{
//...
_Disposed = true; //note this too!
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
And I call the following service method passing an instance of the above data contract:
[OperationContract]
[ServiceKnownType(typeof(RegularFileMetadata))]
Guid BeginUpload(FileMetadataBase metadata);
In the implementation of BeginUpload
, I simply save metadata in a dictionary as:
Dictionary<Guid, RegularFileMetadata> _Dict;
public Guid BeginUpload(FileMetadataBase fileMetadata)
{
//...
var metadata = fileMetadata as RegularFileMetadata;
Guid sessionId = Guid.NewGuid();
_Dict.Add(sessionId, metadata); //metadata SAVED!
return sessionId ;
}
My question is, immediately after returning from this method, why Dispose()
is called even though I've saved the instance in the dictionary _Dict
?
I have verified that Dispose()
method is called on the same instance which I have saved in my dictionary, as _Disposed
becomes true
for the saved object, i.e _Dict[sessionId]._Disposed
becomes true
!
The service behavior of my service is set as:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]