将BeginExecuteNonQuery与异步任务一起使用

时间:2013-08-08 17:00:49

标签: c# .net asynchronous task

我正在努力向基础存储库添加一些方法来处理将处理使用异步任务的查询的其他代码。我基本上只围绕现有的 BeginExecuteNonQuery EndExecuteNonQuery 方法进行任务

使用这样的方法有任何陷阱吗?捕获在AsyncCallback中执行SQL语句期间发生的异常是否正确?

public class Repo
{
    public class SqlCommandAsyncState<T>
    {
        public SqlCommand SqlCommand { get; set; }
        public TaskCompletionSource<T> TaskCompletionSource { get; set; }

        public SqlCommandAsyncState()
        {
            TaskCompletionSource = new TaskCompletionSource<T>();
        }
    }

    private int? sqlCommand_EndExecuteNonQuery(SqlCommand sqlCommand, IAsyncResult result)
    {

        try
        {

            return sqlCommand.EndExecuteNonQuery(result);
        }
        catch (Exception ex)
        {

            // LogError(LogImportance.Fatal, ex);
        }
        finally
        {
            sqlCommand.Connection.Dispose();
        }

        return null;
    }

    private void sqlCommand_ExecuteNonQueryForIntCompleted(IAsyncResult result)
    {
        SqlCommandAsyncState<int?> state = result.AsyncState as SqlCommandAsyncState<int?>;
        state.TaskCompletionSource.SetResult(sqlCommand_EndExecuteNonQuery(state.SqlCommand, result));
    }

    private void sqlCommand_ExecuteNonQueryForBoolCompleted(IAsyncResult result)
    {
        SqlCommandAsyncState<bool?> state = result.AsyncState as SqlCommandAsyncState<bool?>;
        int? rowsAffected = sqlCommand_EndExecuteNonQuery(state.SqlCommand, result);
        state.TaskCompletionSource.SetResult(rowsAffected.HasValue ? (bool?)(rowsAffected.Value > 0) : null);
    }

    public async Task<bool?> UpdateAsync(string commandText, SqlParameter[] parameters)
    {
        SqlCommandAsyncState<bool?> state = new SqlCommandAsyncState<bool?>();

        SqlConnection sql_Connection = new SqlConnection(ConnectionString);

        try
        {
            sql_Connection.Open();
            state.SqlCommand = new SqlCommand(commandText, sql_Connection);
            state.SqlCommand.CommandType = CommandType.StoredProcedure;
            state.SqlCommand.Parameters.AddRange(parameters);
            state.SqlCommand.BeginExecuteNonQuery(new AsyncCallback(sqlCommand_ExecuteNonQueryForBoolCompleted), state);
        }
        catch (Exception ex)
        {
            // LogError(LogImportance.Fatal, ex);
            state.TaskCompletionSource.SetResult(null);
        }

        return await state.TaskCompletionSource.Task;
    }

    public async Task<int?> UpdateWithCountAsync(string commandText, SqlParameter[] parameters)
    {
        SqlCommandAsyncState<int?> state = new SqlCommandAsyncState<int?>();

        SqlConnection sql_Connection = new SqlConnection(ConnectionString);

        try
        {
            sql_Connection.Open();
            state.SqlCommand = new SqlCommand(commandText, sql_Connection);
            state.SqlCommand.CommandType = CommandType.StoredProcedure;
            state.SqlCommand.Parameters.AddRange(parameters);
            state.SqlCommand.BeginExecuteNonQuery(new AsyncCallback(sqlCommand_ExecuteNonQueryForIntCompleted), state);
        }
        catch (Exception ex)
        {
            // LogError(LogImportance.Fatal, ex);
            state.TaskCompletionSource.SetResult(null);
        }

        return await state.TaskCompletionSource.Task;
    }
}

1 个答案:

答案 0 :(得分:1)

Task.Factory.FromAsync旨在实现这一目标。