From 2e027e61957dfa7354bb42bb6bba303e1b7700cf Mon Sep 17 00:00:00 2001 From: Niels Hartvig Date: Thu, 2 Mar 2017 10:33:00 +0100 Subject: [PATCH 1/3] Removes async from filestream instance --- src/Umbraco.Web/umbraco.presentation/content.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Web/umbraco.presentation/content.cs b/src/Umbraco.Web/umbraco.presentation/content.cs index a7c5a75337..a7431ffae9 100644 --- a/src/Umbraco.Web/umbraco.presentation/content.cs +++ b/src/Umbraco.Web/umbraco.presentation/content.cs @@ -844,7 +844,7 @@ namespace umbraco internal void SaveXmlToFile() { LogHelper.Info("Save Xml to file..."); - + //Thread.Sleep(30000); try { var xml = _xmlContent; // capture (atomic + volatile), immutable anyway @@ -861,7 +861,7 @@ namespace umbraco Directory.CreateDirectory(directoryName); // save - using (var fs = new FileStream(_xmlFileName, FileMode.Create, FileAccess.Write, FileShare.Read, bufferSize: 4096, useAsync: true)) + using (var fs = new FileStream(_xmlFileName, FileMode.Create, FileAccess.Write, FileShare.Read)) { SaveXmlToStream(xml, fs); } From 7aa00ee6e09958cfb920c575740d8f15e1f848db Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 2 Mar 2017 10:37:04 +0100 Subject: [PATCH 2/3] Removes unused method that also uses the async flag for file stream - which we know in some cases causes fcn issues, fixes streams not being closed property if there are exceptions --- src/Umbraco.Core/XmlExtensions.cs | 26 ------------------- .../umbraco.presentation/template.cs | 24 ++++++++--------- 2 files changed, 12 insertions(+), 38 deletions(-) diff --git a/src/Umbraco.Core/XmlExtensions.cs b/src/Umbraco.Core/XmlExtensions.cs index e2518c791a..8707523615 100644 --- a/src/Umbraco.Core/XmlExtensions.cs +++ b/src/Umbraco.Core/XmlExtensions.cs @@ -16,32 +16,6 @@ namespace Umbraco.Core /// internal static class XmlExtensions { - /// - /// Saves the xml document async - /// - /// - /// - /// - public static async Task SaveAsync(this XmlDocument xdoc, string filename) - { - if (xdoc.DocumentElement == null) - throw new XmlException("Cannot save xml document, there is no root element"); - - using (var fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read, bufferSize: 4096, useAsync: true)) - using (var xmlWriter = XmlWriter.Create(fs, new XmlWriterSettings - { - Async = true, - Encoding = Encoding.UTF8, - Indent = true - })) - { - //NOTE: There are no nice methods to write it async, only flushing it async. We - // could implement this ourselves but it'd be a very manual process. - xdoc.WriteTo(xmlWriter); - await xmlWriter.FlushAsync().ConfigureAwait(false); - } - } - public static bool HasAttribute(this XmlAttributeCollection attributes, string attributeName) { return attributes.Cast().Any(x => x.Name == attributeName); diff --git a/src/Umbraco.Web/umbraco.presentation/template.cs b/src/Umbraco.Web/umbraco.presentation/template.cs index 4290ce2acf..7b8e60dfb6 100644 --- a/src/Umbraco.Web/umbraco.presentation/template.cs +++ b/src/Umbraco.Web/umbraco.presentation/template.cs @@ -88,19 +88,19 @@ namespace umbraco string originalPath = IOHelper.MapPath(VirtualPathUtility.ToAbsolute(MasterPageFile)); string copyPath = IOHelper.MapPath(VirtualPathUtility.ToAbsolute(path)); - FileStream fs = new FileStream(originalPath, FileMode.Open, FileAccess.ReadWrite); - StreamReader f = new StreamReader(fs); - String newfile = f.ReadToEnd(); - f.Close(); - fs.Close(); + string newFile; + using (var fs = new FileStream(originalPath, FileMode.Open, FileAccess.ReadWrite)) + using (var f = new StreamReader(fs)) + { + newFile = f.ReadToEnd(); + newFile = newFile.Replace("MasterPageFile=\"~/masterpages/", "MasterPageFile=\""); + } - newfile = newfile.Replace("MasterPageFile=\"~/masterpages/", "MasterPageFile=\""); - - fs = new FileStream(copyPath, FileMode.Create, FileAccess.Write); - - StreamWriter replacement = new StreamWriter(fs); - replacement.Write(newfile); - replacement.Close(); + using (var fs = new FileStream(copyPath, FileMode.Create, FileAccess.Write)) + using (var replacement = new StreamWriter(fs)) + { + replacement.Write(newFile); + } } return path; From 7e8f6d391fe19ce8819b4c0c54874866458bb390 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 2 Mar 2017 10:48:11 +0100 Subject: [PATCH 3/3] Just to be sure, moves the text replacement outside of the using - though that wouldn't make any diff --- src/Umbraco.Web/umbraco.presentation/content.cs | 1 - src/Umbraco.Web/umbraco.presentation/template.cs | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Web/umbraco.presentation/content.cs b/src/Umbraco.Web/umbraco.presentation/content.cs index a7431ffae9..07f73e88f6 100644 --- a/src/Umbraco.Web/umbraco.presentation/content.cs +++ b/src/Umbraco.Web/umbraco.presentation/content.cs @@ -844,7 +844,6 @@ namespace umbraco internal void SaveXmlToFile() { LogHelper.Info("Save Xml to file..."); - //Thread.Sleep(30000); try { var xml = _xmlContent; // capture (atomic + volatile), immutable anyway diff --git a/src/Umbraco.Web/umbraco.presentation/template.cs b/src/Umbraco.Web/umbraco.presentation/template.cs index 7b8e60dfb6..50d3876f40 100644 --- a/src/Umbraco.Web/umbraco.presentation/template.cs +++ b/src/Umbraco.Web/umbraco.presentation/template.cs @@ -92,10 +92,11 @@ namespace umbraco using (var fs = new FileStream(originalPath, FileMode.Open, FileAccess.ReadWrite)) using (var f = new StreamReader(fs)) { - newFile = f.ReadToEnd(); - newFile = newFile.Replace("MasterPageFile=\"~/masterpages/", "MasterPageFile=\""); + newFile = f.ReadToEnd(); } + newFile = newFile.Replace("MasterPageFile=\"~/masterpages/", "MasterPageFile=\""); + using (var fs = new FileStream(copyPath, FileMode.Create, FileAccess.Write)) using (var replacement = new StreamWriter(fs)) {