Is it safe to call BeginSaveChanges(null, null) to perform a Fire and Forget Async DataContext call?
I noticed when performing many database transactions using BeginSaveChanges(null, null) the last query is sometimes unsuccessful. Is it because the instance of my DataContext died too quickly for the query to be performed?
When I changed my code to use the normal SaveChanges all the queries are successful.
Specifically, I'm performing queries against the Azure Table Storage using a TableServiceContext.
Edit - If the Dis开发者_如何学编程posing of my DataContex is the real problem. What alternatives do I have? Should I wrap making an instance of my datacontext and executing my query in a Task?
First of all, you should always call the matching EndXXX method any BeginXXX method.
Then you should make sure that you don't dispose the object you're calling a BeginXXX method before you have called the EndXXX method.
A save fire-and-forget approach (ignoring the Dispose issue) is to pass EndXXX as callback to BeginXXX:
obj.BeginXXX(callback: obj.EndXXX, state: null);
// -- or --
obj.BeginXXX(callback: asyncResult => obj.EndXXX(asyncResult), state: null);
You still need to make sure that you don't call obj.Dispose();
before all asynchronous operations finished.
Well for one, if you don't call EndXXX you will never get an exception if something goes wrong. You may want to check that first to see if there's an exception being thrown in your last batch.
Using the new C# 5 async/await feature seems like it will be the best way to perform fire and forget method calls.
What's the new C# await feature do?
精彩评论