Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlConnection.OpenAsync() quits without throwing exception

I have following code. the call to connection.OpenAsync() quits the program without any exception. Even the finally on the caller method is not invoked. program is targetting .NET45 Any idea?

Update: Here is the parent code that works with .Wait(). It quits without .Wait() in 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 async method calls:

    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;
        }
    }
like image 639
ksj Avatar asked Feb 13 '14 02:02

ksj


Video Answer


2 Answers

So the issue was that in the code I had chain of async calls but the parent (main) method was not async and did not have await causing the program to quit when Async was called by one of the child method. I added .Wait() to the call to the async method from main method (which is sync) and it worked fine.

Thanks!

like image 192
ksj Avatar answered Sep 20 '22 14:09

ksj


The msdn documentation states this:

Exceptions will be propagated via the returned Task. If the connection timeout time elapses without successfully connecting, the returned Task will be marked as faulted with an Exception. The implementation returns a Task without blocking the calling thread for both pooled and non-pooled connections.

So, the proper use of connection.OpenAsync() will rather look something like this

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
 }
like image 39
Octavian Mărculescu Avatar answered Sep 22 '22 14:09

Octavian Mărculescu