-
Notifications
You must be signed in to change notification settings - Fork 5
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
Settings corrupted on Shutdown #7
Comments
This issue might occur due to file caching happening on the OS side (see documentation here). As described in the linked article, file caching can be disabled for specific files if necessary. In .NET, this option is realized by passing public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
{
JObject jObject = GetJObject();
foreach (SettingsPropertyValue value in collection)
setSettingsValue(jObject, (string) context["GroupName"], value);
try
{
using (var fs = new FileStream(ApplicationSettingsFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 4096, FileOptions.WriteThrough))
using (var sw = new StreamWriter(fs))
{
sw.WriteLine(JsonConvert.SerializeObject(JsonUtility.SortPropertiesAlphabetically(jObject), Formatting.Indented));
}
}
catch { }
} Please let me know if this resolves the issue you describe. If that's the case, I can consider adding a built-in option to enable this behavior. |
Saving settings like this I have noticed that the resulting file would sometimes have some duplication
I would change settings and just call once It is not json serialization issue because saving two files at the same time will still produce the correct file with
The fix seems to be using I also suggest using But according to this
so that might be the only way to guarantee that corruption won't happen at some point anyway. I will release a version with these changes and see if I get any additional corruption issues in telemetry. |
I occasionally get error in telemetry reports that settings file is corrupted if user shuts down computer while program is running. I already have in place shutdown blocking - program blocks shutdown with
ShutdownBlockReasonCreate
, runsProperties.Settings.Default.Save();
and then removes the shutdown block withShutdownBlockReasonDestroy
. I know that the user didn't press "shut down anyway" because the Serilog logs a message that theShutdownBlockReasonDestroy
was reached and completed, and that log is saved to disk and properly flushed, but for some reason the settings are not consistently saved.Is there a way to ensure that settings are saved?
I am using
PortableJsonSettingsProvider
0.2.1 from NugetThe text was updated successfully, but these errors were encountered: