-
Notifications
You must be signed in to change notification settings - Fork 538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SpreadsheetDocument.Dispose() throws System.ObjectDisposedException #1702
Comments
Have you considered moving from manual |
Of course. The original implementation uses a "using" statement. The example is just simplified. |
This is because of behaviour in ZipArchive. This behaviour needs to change, but by way of explanation: although calling SpreadsheetDocument.Save will persist the relevant parts into the ZipArchive, the ZipArchive won't be persisted into its stream. There's a PR to add ZipArchive.Flush to do this, dotnet/runtime#24149. ZipArchive won't flush its content into the stream until the ZipArchive instance is disposed of. This happens when the Package is disposed of, which in turn happens when the SpreadsheetDocument is disposed of. In the sample, you're disposing of the stream, then the SpreadsheetDocument, so when the ZipArchive tries to flush its content into the stream, it throws the ObjectDisposedException you've found. If you dispose of the SpreadsheetDocument before the stream, I think it'll work. The complete fix would be for ZipArchive to implement Flush, then for ZipPackage to use this so that the Dispose behaviour can be reviewed (at the very least, so it doesn't throw if you Dispose straight after calling SpreadsheetDocument.Save.) This needs to wait for the PR linked above though. |
I got it. Thank you! It looks like we cannot do anything in Open XML SDK implementation. Can this issue stay open? |
Describe the bug
SpreadsheetDocument.Dispose() throws System.ObjectDisposedException: Cannot access a closed Stream.
Screenshots
To Reproduce
Observed behavior
Exception is thrown.
System.ObjectDisposedException : Cannot access a closed Stream. at System.IO.MemoryStream.Seek(Int64 offset, SeekOrigin loc) at System.IO.Compression.ZipArchive.WriteFile() at System.IO.Compression.ZipArchive.Dispose(Boolean disposing) at System.IO.Compression.ZipArchive.Dispose() at System.IO.Packaging.ZipPackage.Dispose(Boolean disposing) at System.IO.Packaging.Package.System.IDisposable.Dispose() at System.IO.Packaging.Package.Close() at DocumentFormat.OpenXml.Features.StreamPackageFeature.Dispose(Boolean disposing) at DocumentFormat.OpenXml.Features.StreamPackageFeature.Dispose() at DocumentFormat.OpenXml.Packaging.PackageFeatureCollection.DocumentFormat.OpenXml.Features.IContainerDisposableFeature.Dispose() at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose(Boolean disposing) at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose()
Expected behavior
Should not throw exception, according to:https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1065
A System.IDisposable.Dispose method should not throw an exception. Dispose is often called as part of the cleanup logic in a finally clause. Therefore, explicitly throwing an exception from Dispose forces the user to add exception handling inside the finally clause.
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: