-2

I'm having trouble getting an async-await call to work. I'm getting an error message that says ... does not contain a definition for 'GetAwaiter' .... Here's my code:

private async Task<IList<MyType>> Load()
{
    SqlInsightDbProvider.RegisterProvider();
    using (var conn = new SqlConnection("connString"))
    {
        //compiler error here
        return await conn.QuerySqlAsync<MyType>("SELECT * FROM tbl");
    }
}

That call to QuerySqlAsync comes from InsightDB. The signature for it is:

public static Task<IList<T1>> QuerySqlAsync<T1>(
    this IDbConnection connection, 
    string sql, 
    object parameters = null, 
    CommandBehavior commandBehavior = CommandBehavior.Default, 
    int? commandTimeout = default(int?), 
    IDbTransaction transaction = null, 
    CancellationToken? cancellationToken = default(CancellationToken?),
    object outputParameters = null);

I have no experience with async-await and I thought that this would be a good, non-contrived example I can use to get familiar with it. I thought that an I/O operation like a database call is an archetypal example of when to use async-await. I've searched on StackOverflow for how to fix this, but I must not be understanding the answers I found.

Edit: Here's the error message I get: error CS1061: 'Task<IList<MyType>>' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'Task<IList<MyType>>' could be found (are you missing a using directive or an assembly reference?)

user2023861
  • 8,030
  • 9
  • 57
  • 86
  • What exactly is the `...` in the error message? Please post the *exact* error message in your question. – Jon Skeet May 05 '17 at 17:16
  • @JonSkeet I just added the error message. – user2023861 May 05 '17 at 17:23
  • What version of .NET are you targeting? If you're using .NET 4 or later, it should be fine... – Jon Skeet May 05 '17 at 17:28
  • @JonSkeet it's .Net Framework 4. I tried building with 4.5 and 4.6 just to check, and I get the same error message. – user2023861 May 05 '17 at 17:31
  • I'm not surprised about it failing against .NET 4.0 unless you have the appropriate extra nuget package (whose name I can't remember offhand), but I suspect didn't do enough of a "clean" build when trying against 4.5/4.6, where it really *should* work. I would try that again... – Jon Skeet May 05 '17 at 17:33
  • 1
    @JonSkeet You must be talking about `Install-Package Microsoft.Bcl.Async`. I ran that and the code builds! Thanks (Maybe it didn't build for me in 4.5/4.6 because I referenced some other libraries that use 4.0?) – user2023861 May 05 '17 at 17:36
  • 1
    Yes, that would be the one - but I would recommend moving to .NET 4.6 instead, to be honest... One lesson to learn: when asking a question, if you're on a platform version which was superceded several years ago, please mention it in the question... (And no, those references are probably irrelevant - I suspect it was just a matter of not cleaning thoroughly.) – Jon Skeet May 05 '17 at 17:37
  • @JonSkeet, our build servers haven't been upgraded to 4.6. Some of them might be using 4.5. I've been doing most of my development in 4.0 and I can't complain. – user2023861 May 05 '17 at 17:41
  • 1
    Well if you're really happy running unsupported code... see https://blogs.msdn.microsoft.com/dotnet/2015/12/09/support-ending-for-the-net-framework-4-4-5-and-4-5-1/ – Jon Skeet May 05 '17 at 17:53

1 Answers1

0

The answer was to include this package in my project:

Install-Package Microsoft.Bcl.Async

I need this because I'm using .Net 4.0.

user2023861
  • 8,030
  • 9
  • 57
  • 86