json-flatfile-datastore
json-flatfile-datastore copied to clipboard
System.OperationCanceledException When Disposing DataStore
I'm using your library to implement a set of local data stores. I love this library and the implementation. I am seeing a behavior I am trying to resolve as I am implementing unhandled exception handling now in the my .NET 6 WPF application. So this keeps tripping it...
Here is an example of one of my methods on one of the repositories:
public IEnumerable<QuestionSet> GetAll()
{
using (var ds = new DataStore(_fileName, true, nameof(QuestionSet.PrimaryKey)))
{
var coll = ds.GetCollection<QuestionSet>();
return coll.AsQueryable().ToList();
}
}
After the 'using' exits I always get a: System.OperationCanceledException
If I don't use 'using' or do not Dispose, I don't see the exception.
Should I not be wrapping the DataStore object in a using? Or am I using the library in the wrong way?
Hi! Nice to hear that you have found use for the library :)
The recommended way to use DataStore is to create a single instance for the whole application lifecycle. You would only need to dispose the datastore when you close the application.
Nevertheless, I will inspect if I find the reason for the System.OperationCanceledException.
Thank you for the hint about the DataStore. I will implement a singleton to hold a DataStore instance.
Looking at the code it looks like this may call the exception to be thrown?: https://github.com/ttu/json-flatfile-datastore/blob/24ad435fe834c9aaba6c64bfbab7bff53c48ef82/JsonFlatFileDataStore/DataStore.cs#L145
Looking at this though (https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-cancellation) it seems that should only occur if something awaiting on the task? Have not done much work with cancellation token before....
Thanks for pointing me forward. Yes, CancellationToken is then the reason for this Exception.
It is only used with BlockingCollection in the background process, which executes the store updates and updates the JSON file. https://github.com/ttu/json-flatfile-datastore/blob/24ad435fe834c9aaba6c64bfbab7bff53c48ef82/JsonFlatFileDataStore/DataStore.cs#L86
Apparently, at least some of the BlockingCollection methods throw that exception
https://stackoverflow.com/questions/8953407/using-blockingcollectiont-operationcanceledexception-is-there-a-better-way
I will leave this Issue open and inspect this topic more.
I'm getting this a lot too, every time I'm closing it, actually. It's a singleton, but I load a different file based on what's needed. But I'm manually calling Dispose before instantiating it again, maybe that's not the right way to do it?
edit: it's also being done from a separate thread
Thanks for the comment! That should be the right way if you need to change the file based on your need. What .NET version are you using and are you developing for web or desktop?
This might be a fix for this: https://github.com/ttu/json-flatfile-datastore/pull/78
Hi @ttu sorry for the delayed response, just getting back to this. Thanks for the fix! I stopped calling Dispose() manually and that ended up fixing it too, but maybe there were still some side-effects which are fixed. I'll upgrade the library today! Thanks again!
@nabeelio Good that you found a workaround👍
New version with the fix in now released https://www.nuget.org/packages/JsonFlatFileDataStore/2.4.1