While studying the following snippet of code from the project I work on
var storedObject = _objectStorage.OpenObject(objectId);
PerformActions(storeObject);
I discovered that storedObject object is disposable and need to be disposed.
My first try was to enclose PerformActions
in using
statement:
using (var storedObject = _objectStorage.OpenObject(objectId))
{
PerformActions(storeObject);
}
but as it turns out at least one action inside PerformActions
is asynchronous and therefore I get ObjectDisposedException
.
What refactoring strategy would you suggest for this case to eliminate issue with undisposed objects?