Skip to content
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

Fixed issues #26, #31, #33 where running multiple instances cause var… #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Source/CSharpAnalytics/AutoMeasurement/BaseAutoMeasurement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,20 @@ protected async Task StartRequesterAsync()
/// <summary>
/// Suspend the requester and preserve any unsent URIs.
/// </summary>
/// <returns>Task that completes when the requester has been suspended.</returns>
protected async Task StopRequesterAsync()
protected void StopRequesterAsync()
{
var safeBackgroundRequester = backgroundRequester;
if (safeBackgroundRequester == null) return;

var recentRequestsToPersist = new List<Uri>();
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ protected override bool IsInternetAvailable()
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private async void ApplicationOnExit(object sender, EventArgs eventArgs)
private void ApplicationOnExit(object sender, EventArgs eventArgs)
{
UnhookEvents();
await StopRequesterAsync();
StopRequesterAsync();
}
}
}
4 changes: 2 additions & 2 deletions Source/CSharpAnalytics/AutoMeasurement/WpfAutoMeasurement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ protected override bool IsInternetAvailable()
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private async void ApplicationOnExit(object sender, EventArgs eventArgs)
private void ApplicationOnExit(object sender, EventArgs eventArgs)
{
UnhookEvents();
await StopRequesterAsync();
StopRequesterAsync();
}
}
}
48 changes: 24 additions & 24 deletions Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Xml;

namespace CSharpAnalytics.Serializers
{
Expand Down Expand Up @@ -48,36 +49,32 @@ private static string GetDefaultFolderPath()
public static async Task<T> Restore<T>(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<T>(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)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this changes the underlying format? If so then this change causes all clients to use their current information and be treated as new users again :(

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In matter of fact it doesn't change anything regarding the data. The previous format was a non-intended xml and by using XmlWriterSettings it produces an intended xml. The serializer (DataContractSerializer) didn't changed thus the objects get serialized and deserialized as before.

if (deleteBadData)
File.Delete(file);
}

return default(T);
}
catch (FileNotFoundException)
{
Expand All @@ -95,20 +92,23 @@ public static async Task<T> Restore<T>(string filename = null, bool deleteBadDat
public static async Task Save<T>(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<T>(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);
}
}

/// <summary>
Expand All @@ -125,4 +125,4 @@ private static string GetFilePath<T>(string filename)
return Path.Combine(FolderPath, filename ?? typeof(T).Name);
}
}
}
}