From 7d5c2a4cd8c9a407cd6bacff59c1863c103c5eda Mon Sep 17 00:00:00 2001 From: Tom Jebo Date: Wed, 29 Mar 2023 17:04:23 -0700 Subject: [PATCH 1/4] Remove OpenXmlPackage.Close and all references --- CHANGELOG.md | 1 + .../PowerPointModernCommentSample/Program.cs | 4 +-- samples/SunburstChartExample/Program.cs | 5 +-- samples/ThreadedCommentExample/Program.cs | 5 +-- .../Packaging/OpenXmlPackage.cs | 9 ----- .../CreateFromTemplateTests.cs | 6 ++-- .../SaveAndCloneTests.cs | 35 +++++++++++-------- .../ofapiTest/OpenXmlValidatorTest.cs | 2 -- 8 files changed, 27 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c33231b2..acd123627 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Removed obsolete validation logic from v1 of the SDK - Removed obsoleted methods from 2.x - Removed mutable properties on OpenXmlAttribute and marked as `readonly` +- Removed `OpenXmlPackage.Close` in favor of Dispose (#1298) ### Fixed - Fixed incorrect file extensions for vbaProject files (#1292) diff --git a/samples/PowerPointModernCommentSample/Program.cs b/samples/PowerPointModernCommentSample/Program.cs index 5104b11d1..3224eb959 100644 --- a/samples/PowerPointModernCommentSample/Program.cs +++ b/samples/PowerPointModernCommentSample/Program.cs @@ -38,7 +38,7 @@ public static void Main(string[] args) } // Create a presentation - PresentationDocument presentationDocument = PowerPointUtils.CreatePresentation(fileInfo.FullName); + using PresentationDocument presentationDocument = PowerPointUtils.CreatePresentation(fileInfo.FullName); // create missing PowerPointAuthorsPart if it is null if (presentationDocument?.PresentationPart?.authorsPart is null) @@ -94,8 +94,6 @@ public static void Main(string[] args) new CommentRelationship() { Id = slidePart.GetIdOfPart(powerPointCommentPart) }) { Uri = "{6950BFC3-D8DA-4A85-94F7-54DA5524770B}" })); - - presentationDocument.Close(); } catch (Exception ex) { diff --git a/samples/SunburstChartExample/Program.cs b/samples/SunburstChartExample/Program.cs index 6d87858d8..bbefe9bae 100644 --- a/samples/SunburstChartExample/Program.cs +++ b/samples/SunburstChartExample/Program.cs @@ -26,14 +26,11 @@ private static void Main(string[] args) public static void CreatePresentation(string filepath) { // Create a presentation at a specified file path. The presentation document type is pptx, by default. - var presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation); + using PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation); var presentationPart = presentationDoc.AddPresentationPart(); presentationPart.Presentation = new Presentation(); CreatePresentationParts(presentationPart); - - // Close the presentation handle - presentationDoc.Close(); } private static void CreatePresentationParts(PresentationPart presentationPart) diff --git a/samples/ThreadedCommentExample/Program.cs b/samples/ThreadedCommentExample/Program.cs index 5d40eed3c..b6f169712 100644 --- a/samples/ThreadedCommentExample/Program.cs +++ b/samples/ThreadedCommentExample/Program.cs @@ -51,7 +51,7 @@ private static void Main(string[] args) } // WORKBOOK - SpreadsheetDocument? sd = ExampleUtilities.CreateSpreadsheetWorkbook(filePath, sheetName); + using SpreadsheetDocument? sd = ExampleUtilities.CreateSpreadsheetWorkbook(filePath, sheetName); if (sd != null) { @@ -105,9 +105,6 @@ private static void Main(string[] args) // ThreadedComment attributes { Ref = reference, PersonId = idUser, Id = tcId, DT = System.DateTime.Now }); } - - // Close the document. - sd.Close(); } else { diff --git a/src/DocumentFormat.OpenXml.Framework/Packaging/OpenXmlPackage.cs b/src/DocumentFormat.OpenXml.Framework/Packaging/OpenXmlPackage.cs index d1fb78621..0cd463770 100644 --- a/src/DocumentFormat.OpenXml.Framework/Packaging/OpenXmlPackage.cs +++ b/src/DocumentFormat.OpenXml.Framework/Packaging/OpenXmlPackage.cs @@ -143,15 +143,6 @@ public void DeletePartsRecursivelyOfType() DeletePartsRecursivelyOfTypeBase(); } - /// - /// Saves and closes the OpenXml package and all underlying part streams. - /// - public void Close() - { - ThrowIfObjectDisposed(); - Dispose(); - } - #region methods to operate DataPart /// diff --git a/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs b/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs index f2b0f3297..08380aed3 100644 --- a/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs +++ b/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs @@ -20,7 +20,7 @@ public void CanCreatePresentationFromTemplate() var part = packageDocument.PresentationPart; var root = part.Presentation; - packageDocument.SaveAs(Path.GetTempFileName()).Close(); + using var temp = packageDocument.SaveAs(Path.GetTempFileName()); // We are fine if we have not run into an exception. Assert.True(true); @@ -36,7 +36,7 @@ public void CanCreateSpreadsheetFromTemplate() var part = packageDocument.WorkbookPart; var root = part.Workbook; - packageDocument.SaveAs(Path.GetTempFileName()).Close(); + using var temp = packageDocument.SaveAs(Path.GetTempFileName()); // We are fine if we have not run into an exception. Assert.True(true); @@ -52,7 +52,7 @@ public void CanCreateWordprocessingDocumentFromTemplate() var part = packageDocument.MainDocumentPart; var root = part.Document; - packageDocument.SaveAs(Path.GetTempFileName()).Close(); + using var temp = packageDocument.SaveAs(Path.GetTempFileName()); // We are fine if we have not run into an exception. Assert.True(true); diff --git a/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs b/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs index 177cb44d5..21c27bcb2 100644 --- a/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs +++ b/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs @@ -33,7 +33,10 @@ public void CanCloneDocument() { var body = clone.MainDocumentPart.Document.Body; body.InsertBefore(new Paragraph(new Run(new Text("Hello World"))), body.FirstChild); - clone.SaveAs(file.Path).Close(); + using (var temp = clone.SaveAs(file.Path)) + { + // clean up the temp package. + } using (var dest = WordprocessingDocument.Open(file.Path, false)) { @@ -105,18 +108,20 @@ public void CanDoMultithreadedMultipleCloning() } // Clone clone1 again. - var clone3 = (WordprocessingDocument)clone1.Clone(); - var body3 = clone3.MainDocumentPart.Document.Body; - body3.GetFirstChild() - .InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 3")))); - clone3.Close(); + using (WordprocessingDocument clone3 = (WordprocessingDocument)clone1.Clone()) + { + var body3 = clone3.MainDocumentPart.Document.Body; + body3.GetFirstChild() + .InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 3")))); + } // Clone source again. - var clone4 = (WordprocessingDocument)source.Clone(); - var body4 = clone4.MainDocumentPart.Document.Body; - body4.GetFirstChild() - .InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 4")))); - clone4.Close(); + using (WordprocessingDocument clone4 = (WordprocessingDocument)source.Clone()) + { + var body4 = clone4.MainDocumentPart.Document.Body; + body4.GetFirstChild() + .InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 4")))); + } } }); } @@ -181,7 +186,7 @@ public void CanWildlyCloneAndFlush() using (var tempFile = TemporaryFile.Create()) { - clone.SaveAs(tempFile.Path).Close(); + using var temp = clone.SaveAs(tempFile.Path); } } }); @@ -197,7 +202,7 @@ public void CanDoPackageBasedCloningWord() { using (var package = Package.Open(ms, FileMode.Create)) { - source.Clone(package).Close(); + using var temp = source.Clone(package); } ms.Position = 0; @@ -218,7 +223,7 @@ public void CanDoPackageBasedCloningSpreadsheet() { using (var package = Package.Open(ms, FileMode.Create)) { - source.Clone(package).Close(); + using var temp = source.Clone(package); } ms.Position = 0; @@ -239,7 +244,7 @@ public void CanDoPackageBasedCloningPowerpoint() { using (var package = Package.Open(ms, FileMode.Create)) { - source.Clone(package).Close(); + using var temp = source.Clone(package); } ms.Position = 0; diff --git a/test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlValidatorTest.cs b/test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlValidatorTest.cs index 1346bf689..ee6014d24 100644 --- a/test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlValidatorTest.cs +++ b/test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlValidatorTest.cs @@ -3079,8 +3079,6 @@ public void PackageStuctureValidatingTest() commentsPart.Comments.SaveToPart(commentsPart2); mainPart.Relationships.Create(mainPart.Uri, System.IO.Packaging.TargetMode.Internal, mainPart.RelationshipType); - - wordDoc.Close(); } stream.Flush(); From 56b28c2ea5c3c15d6375e504fe508dad7bde1025 Mon Sep 17 00:00:00 2001 From: Tom Jebo Date: Thu, 30 Mar 2023 14:34:12 -0700 Subject: [PATCH 2/4] changed issue 1298 to PR 1373 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acd123627..626e89117 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Removed obsolete validation logic from v1 of the SDK - Removed obsoleted methods from 2.x - Removed mutable properties on OpenXmlAttribute and marked as `readonly` -- Removed `OpenXmlPackage.Close` in favor of Dispose (#1298) +- Removed `OpenXmlPackage.Close` in favor of Dispose (#1373) ### Fixed - Fixed incorrect file extensions for vbaProject files (#1292) From 54f9ab9ec11176355f7f50df666964cafc26a083 Mon Sep 17 00:00:00 2001 From: Tom Jebo Date: Thu, 30 Mar 2023 14:43:48 -0700 Subject: [PATCH 3/4] refactor use of temp variable to use the discard variable --- .../CreateFromTemplateTests.cs | 6 +++--- test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs b/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs index 08380aed3..f1d64a78d 100644 --- a/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs +++ b/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs @@ -20,7 +20,7 @@ public void CanCreatePresentationFromTemplate() var part = packageDocument.PresentationPart; var root = part.Presentation; - using var temp = packageDocument.SaveAs(Path.GetTempFileName()); + using var _ = packageDocument.SaveAs(Path.GetTempFileName()); // We are fine if we have not run into an exception. Assert.True(true); @@ -36,7 +36,7 @@ public void CanCreateSpreadsheetFromTemplate() var part = packageDocument.WorkbookPart; var root = part.Workbook; - using var temp = packageDocument.SaveAs(Path.GetTempFileName()); + using var _ = packageDocument.SaveAs(Path.GetTempFileName()); // We are fine if we have not run into an exception. Assert.True(true); @@ -52,7 +52,7 @@ public void CanCreateWordprocessingDocumentFromTemplate() var part = packageDocument.MainDocumentPart; var root = part.Document; - using var temp = packageDocument.SaveAs(Path.GetTempFileName()); + using var _ = packageDocument.SaveAs(Path.GetTempFileName()); // We are fine if we have not run into an exception. Assert.True(true); diff --git a/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs b/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs index 21c27bcb2..a3a0bb5b8 100644 --- a/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs +++ b/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs @@ -186,7 +186,7 @@ public void CanWildlyCloneAndFlush() using (var tempFile = TemporaryFile.Create()) { - using var temp = clone.SaveAs(tempFile.Path); + using var _ = clone.SaveAs(tempFile.Path); } } }); @@ -202,7 +202,7 @@ public void CanDoPackageBasedCloningWord() { using (var package = Package.Open(ms, FileMode.Create)) { - using var temp = source.Clone(package); + using var _ = source.Clone(package); } ms.Position = 0; @@ -223,7 +223,7 @@ public void CanDoPackageBasedCloningSpreadsheet() { using (var package = Package.Open(ms, FileMode.Create)) { - using var temp = source.Clone(package); + using var _ = source.Clone(package); } ms.Position = 0; @@ -244,7 +244,7 @@ public void CanDoPackageBasedCloningPowerpoint() { using (var package = Package.Open(ms, FileMode.Create)) { - using var temp = source.Clone(package); + using var _ = source.Clone(package); } ms.Position = 0; From 511905cb30a75701f2951dd50fe9cde65f92b2c8 Mon Sep 17 00:00:00 2001 From: Tom Jebo Date: Sun, 2 Apr 2023 21:05:08 -0700 Subject: [PATCH 4/4] refactor discard usage in tests --- .../CreateFromTemplateTests.cs | 6 +++--- test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs b/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs index f1d64a78d..2f8f6ae2c 100644 --- a/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs +++ b/test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs @@ -20,7 +20,7 @@ public void CanCreatePresentationFromTemplate() var part = packageDocument.PresentationPart; var root = part.Presentation; - using var _ = packageDocument.SaveAs(Path.GetTempFileName()); + _ = packageDocument.SaveAs(Path.GetTempFileName()); // We are fine if we have not run into an exception. Assert.True(true); @@ -36,7 +36,7 @@ public void CanCreateSpreadsheetFromTemplate() var part = packageDocument.WorkbookPart; var root = part.Workbook; - using var _ = packageDocument.SaveAs(Path.GetTempFileName()); + _ = packageDocument.SaveAs(Path.GetTempFileName()); // We are fine if we have not run into an exception. Assert.True(true); @@ -52,7 +52,7 @@ public void CanCreateWordprocessingDocumentFromTemplate() var part = packageDocument.MainDocumentPart; var root = part.Document; - using var _ = packageDocument.SaveAs(Path.GetTempFileName()); + _ = packageDocument.SaveAs(Path.GetTempFileName()); // We are fine if we have not run into an exception. Assert.True(true); diff --git a/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs b/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs index a3a0bb5b8..b162f6b3a 100644 --- a/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs +++ b/test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs @@ -186,7 +186,9 @@ public void CanWildlyCloneAndFlush() using (var tempFile = TemporaryFile.Create()) { - using var _ = clone.SaveAs(tempFile.Path); + using (clone.SaveAs(tempFile.Path)) + { + } } } }); @@ -202,7 +204,7 @@ public void CanDoPackageBasedCloningWord() { using (var package = Package.Open(ms, FileMode.Create)) { - using var _ = source.Clone(package); + _ = source.Clone(package); } ms.Position = 0; @@ -223,7 +225,7 @@ public void CanDoPackageBasedCloningSpreadsheet() { using (var package = Package.Open(ms, FileMode.Create)) { - using var _ = source.Clone(package); + _ = source.Clone(package); } ms.Position = 0; @@ -244,7 +246,7 @@ public void CanDoPackageBasedCloningPowerpoint() { using (var package = Package.Open(ms, FileMode.Create)) { - using var _ = source.Clone(package); + _ = source.Clone(package); } ms.Position = 0;