问题
I've looked numerous posts on task exception handling and I'm not clear how to resolve this issue. My application crashes anytime there is an exception in one of my tasks. I am unable to catch the exception and my application is left in an inoperable state.
UPDATE: This only seems to happen when calling the ExecuteAsync method in the DataStax Cassandra C# driver. This leads me to believe it's an issue in the driver itself. When I create my own task and throw an exception it works fine.
Most use cases seem to await all asynchronous calls, but in my case I need to fire off a group of asynchronous commands and then use WhenAll to await them together (rather than awaiting each one individually).
This is based off of this post which shows how to batch up tasks to send to a Cassandra database: https://lostechies.com/ryansvihla/2014/08/28/cassandra-batch-loading-without-the-batch-keyword/
This is the same practice recommended by Microsoft when you want to perform multiple async requests without having to chain them: https://social.msdn.microsoft.com/Forums/en-US/6ab8c611-6b0c-4390-933c-351e56b62526/await-multiple?forum=async
My application entry point:
public void Go()
{
dbTest().Wait();
My async method...
private async Task dbTest() {
List<Task> tasks = new List<Task>();
Task<RowSet> resultSetFuture = session.ExecuteAsync(bind); // spawn a db exception
Task<RowSet> resultSetFuture2 = session.ExecuteAsync(bind);
Task<RowSet> resultSetFuture3 = session.ExecuteAsync(bind);
tasks.Add(resultSetFuture);
tasks.Add(resultSetFuture2);
tasks.Add(resultSetFuture3);
try
{
await Task.WhenAll(tasks.ToArray());
}
catch (Exception ex)
{
...
}
All indications are that WhenAll should properly catch any exceptions from async methods, but it just locks up my application in this case.
来源:https://stackoverflow.com/questions/30698379/handling-exceptions-for-deferred-tasks-in-cassandra