SqlConnection.OpenAsync () exits without exception

I have the following code. the connection.OpenAsync () call exits without any exceptions. Even in the end, the caller method is not called. The program targets .NET45. Any idea?

Update. Here is the parent code that works with .Wait (). It exits without .Wait () in the parent code when connection.OpenAsync () is called in the child method below.

        static void Main(string[] args)
        {
            UpdateSqlDatabase updateSqlDatabase = new UpdateSqlDatabase(args);
            updateSqlDatabase.UpdateDatabaseSchemaAsync().Wait();
        }

After a series of calls to the asynchronous method:

    public async Task<T> ExecuteQueryAsync<T>(string connectionString, string commandText, IDictionary<string, object> parameters, Func<SqlDataReader, T> rowMapFunc)
    {
        using (var connection = new SqlConnection(connectionString))
        {
            try
            {
                await connection.OpenAsync();
            }
            catch (Exception ex)
            {
            }

            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = commandText;
            if (parameters != null)
            {
                foreach (var item in parameters)
                {
                    command.Parameters.AddWithValue(item.Key, item.Value);
                }
            }

            SqlDataReader reader = await command.ExecuteReaderAsync();
            T retObj = default(T);

            while (await reader.ReadAsync())
            {
                retObj = rowMapFunc(reader);
            }

            return retObj;
        }
    }
+3
source share
3 answers

, , , () , , Async . .Wait() async ( ), .

!

+2

msdn documentation :

. , return . , .

, connection.OpenAsync() :

using(var connection = new SqlConnection(connectionString))
{
    var connectionTask = connection.OpenAsync();
    // other code goes here
    Task.WaitAll(connectionTask); //make sure the task is completed
    if(connectionTask.IsFaulted) // in case of failure
    {
       throw new Exception("Connection failure", connectionTask.Exception);
    }
    // rest of the code
 }
+1

Perhaps the method OpenAsync()calls Enviroment.FailFast(), which will result in any final blocks not being executed. This usage is recorded in the event log, so you should check to see if there is anything there.

For more information, see the following http://msdn.microsoft.com/en-us/library/ms131100(v=vs.110).aspx

0
source

All Articles