1

I'm trying to modify an existing database call so that it makes asynchronous database calls. This is my first foray into asynchronous database calls, so I've looked at this post, this post, and the first two sections of this MSDN article. This is the code that I've come up with, which is similar to what's found in the second answer of the second post:

public async Task<IEnumerable<Item>> GetDataAsync(int id)
{
    using (SqlConnection conn = new SqlConnection(oCSB.ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("stored_procedure", conn))
        {
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("param", "value");

            await conn.OpenAsync();
            SqlDataReader reader = await cmd.ExecuteReaderAsync();
            return ReadItems(reader, id).ToList();
        }
    }
}

private IEnumerable<Item> ReadItems(SqlDataReader reader, long id)
{
    while (reader.Read())
    {
        var item = new Item(id);        
        yield return item;
    }
}

The application is a Web Forms application, and the call is initiated by a jQuery ajax request to a static WebMethod in an aspx page, which then calls the GetDataAsync method. Unfortunately, the application hangs on the cmd.ExecuteReaderAsync call with no exception thrown, and I haven't been able to figure out why. I've run it both on the VS dev server and on my local IIS 8, but I get the same result. I've also tried modifying it so that it makes a very simple select on a table. I've also tried changing the code based on other posts I've come across either on MSDN or SO. Anybody know what could possibly be causing it to hang on the ExecuteReaderAsync call?

Community
  • 1
  • 1
dotnetesse
  • 256
  • 6
  • 16
  • .NET version? sync context compatibility setting? Or, are you calling Result or Wait somewhere? – usr Jul 28 '14 at 22:11
  • How are you calling `GetDataAsync()`? – DavidG Jul 28 '14 at 22:19
  • @usr & DavidG, sorry about that. It's .Net 4.5. I'm not sure what sync context compatibility setting refers to. I am calling Result in the WebMethod: return obj.GetDataAsync(id).Result; – dotnetesse Jul 28 '14 at 22:35
  • 1
    Classic deadlock then. Why are you using async when you are waiting on the result using `Result`? Makes no sense. Either go synchronous, or asynchronous. Not wait-for-async. – usr Jul 28 '14 at 22:40
  • @usr, I think I'm taking the wrong approach here. My intention is to speed up record retrieval & iteration. We have scenarios where we're retrieving 10k+ records, then looping through the reader object to create Item objects. – dotnetesse Jul 28 '14 at 23:24
  • So you are hoping to gain parallelism? Yet you await all asynchronous operations killing parallelism. Probably a conceptual problem with your understanding of async "stuff". – usr Jul 28 '14 at 23:30
  • "Async" does not mean "faster". The first thing to check is whether you can filter or page the results (>10k sounds like a whole lot). If not, consider database sharding. Caching is also always an option. – Stephen Cleary Jul 29 '14 at 00:25
  • @StephenCleary, thanks for the tips. I'll continue looking into this. – dotnetesse Jul 29 '14 at 11:32

0 Answers0