diff --git a/Source/CSharpAnalytics/AutoMeasurement/BaseAutoMeasurement.cs b/Source/CSharpAnalytics/AutoMeasurement/BaseAutoMeasurement.cs index ea0aa7e..c01cdba 100644 --- a/Source/CSharpAnalytics/AutoMeasurement/BaseAutoMeasurement.cs +++ b/Source/CSharpAnalytics/AutoMeasurement/BaseAutoMeasurement.cs @@ -202,7 +202,7 @@ protected async Task StartRequesterAsync() /// Suspend the requester and preserve any unsent URIs. /// /// Task that completes when the requester has been suspended. - protected async Task StopRequesterAsync() + protected void StopRequesterAsync() { var safeBackgroundRequester = backgroundRequester; if (safeBackgroundRequester == null) return; @@ -210,12 +210,12 @@ protected async Task StopRequesterAsync() var recentRequestsToPersist = new List(); if (safeBackgroundRequester.IsStarted) { - var pendingRequests = await safeBackgroundRequester.StopAsync(); + var pendingRequests = safeBackgroundRequester.StopAsync().GetAwaiter().GetResult(); recentRequestsToPersist = pendingRequests.Skip(pendingRequests.Count - MaximumRequestsToPersist).ToList(); } - await Save(recentRequestsToPersist, QueueStorageName); - await Save(sessionManager.GetState(), SessionStorageName); + Save(recentRequestsToPersist, QueueStorageName); + Save(sessionManager.GetState(), SessionStorageName); safeBackgroundRequester.Dispose(); backgroundRequester = null; diff --git a/Source/CSharpAnalytics/AutoMeasurement/WinFormsAutoMeasurement.cs b/Source/CSharpAnalytics/AutoMeasurement/WinFormsAutoMeasurement.cs index 71b4bf4..9ed568b 100644 --- a/Source/CSharpAnalytics/AutoMeasurement/WinFormsAutoMeasurement.cs +++ b/Source/CSharpAnalytics/AutoMeasurement/WinFormsAutoMeasurement.cs @@ -105,10 +105,10 @@ protected override bool IsInternetAvailable() /// /// /// - private async void ApplicationOnExit(object sender, EventArgs eventArgs) + private void ApplicationOnExit(object sender, EventArgs eventArgs) { UnhookEvents(); - await StopRequesterAsync(); + StopRequesterAsync(); } } } \ No newline at end of file diff --git a/Source/CSharpAnalytics/AutoMeasurement/WpfAutoMeasurement.cs b/Source/CSharpAnalytics/AutoMeasurement/WpfAutoMeasurement.cs index 48e8346..42e3264 100644 --- a/Source/CSharpAnalytics/AutoMeasurement/WpfAutoMeasurement.cs +++ b/Source/CSharpAnalytics/AutoMeasurement/WpfAutoMeasurement.cs @@ -114,10 +114,10 @@ protected override bool IsInternetAvailable() /// /// /// - private async void ApplicationOnExit(object sender, EventArgs eventArgs) + private void ApplicationOnExit(object sender, EventArgs eventArgs) { UnhookEvents(); - await StopRequesterAsync(); + StopRequesterAsync(); } } } \ No newline at end of file diff --git a/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs b/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs index 1ce8243..c8dc82d 100644 --- a/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs +++ b/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs @@ -3,6 +3,7 @@ using System.Reflection; using System.Runtime.Serialization; using System.Threading.Tasks; +using System.Xml; namespace CSharpAnalytics.Serializers { @@ -48,36 +49,32 @@ private static string GetDefaultFolderPath() public static async Task Restore(string filename = null, bool deleteBadData = false) { var serializer = new DataContractSerializer(typeof(T), new[] { typeof(DateTimeOffset) }); + var settings = new XmlReaderSettings { Async = true }; try { var file = GetFilePath(filename); - try { - using (var inputStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) + using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) + using (var xmlReader = XmlReader.Create(fileStream, settings)) { - if (inputStream.Length == 0) - { - return default(T); - } - - using (var memoryStream = new MemoryStream()) - { - await inputStream.CopyToAsync(memoryStream); - await inputStream.FlushAsync(); - memoryStream.Seek(0, SeekOrigin.Begin); - - return (T)serializer.ReadObject(memoryStream); - } + await xmlReader.ReadAsync(); + return (T)serializer.ReadObject(xmlReader); } } catch (SerializationException) { if (deleteBadData) File.Delete(file); - throw; } + catch (XmlException) + { + if (deleteBadData) + File.Delete(file); + } + + return default(T); } catch (FileNotFoundException) { @@ -95,20 +92,23 @@ public static async Task Restore(string filename = null, bool deleteBadDat public static async Task Save(T value, string filename = null) { var serializer = new DataContractSerializer(typeof(T), new[] { typeof(DateTimeOffset) }); - + var settings = new XmlWriterSettings { Indent = true, Async = true }; var file = GetFilePath(filename); - using (var memoryStream = new MemoryStream()) + try { - serializer.WriteObject(memoryStream, value); - using (var fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true)) + using (var xmlWriter = XmlWriter.Create(fileStream, settings)) { - memoryStream.Seek(0, SeekOrigin.Begin); - await memoryStream.CopyToAsync(fileStream); - await fileStream.FlushAsync(); + serializer.WriteObject(xmlWriter, value); + await xmlWriter.FlushAsync(); } } + catch (UnauthorizedAccessException) + { + System.Diagnostics.Debug.WriteLine( + "Failed to save to {0}. You may have insufficient rights or a synchronization may be occuring.", file); + } } /// @@ -125,4 +125,4 @@ private static string GetFilePath(string filename) return Path.Combine(FolderPath, filename ?? typeof(T).Name); } } -} \ No newline at end of file +}