o async não funciona com Visual Studio 2010 e .NET 4.0? [duplicado]

Sep 19 2020

Eu tenho um código C # abaixo, ele funciona bem no Visual studio 2017 com .Net Framework 4.0, mas quando eu executo o mesmo código-fonte no visual studio 2010 com .net Framework, ele não está funcionando e mostra uma mensagem de erro compilada quase assíncrona e aguarde. Por favor, aconselhe a solução alternativa para fazer isso.

Código fonte :-

static string url = "https://einvoicing.internal.cleartax.co/v2/eInvoice/download?irns=7b46fb61d6e0883bdfdfb997ca25c4b5629d3062241532f6e8e6fcdfc0a95c9a";
static HttpClient client = new HttpClient(); 
static async Task DownloadCurrentAsync(string StrInvoiceNo)
        {

            client.DefaultRequestHeaders.Add("owner_id", "78c6beda-54a2-11ea-b064-0af3f8b02c24");
            client.DefaultRequestHeaders.Add("gstin", "29AAFCD5862R000");
            HttpResponseMessage response = await client.GetAsync(url);
            
            response.EnsureSuccessStatusCode(); // <= Will throw if unsuccessful
            using (FileStream fileStream = new FileStream(StrInvoiceNo.Replace("/","-") + ".pdf", FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //copy the content from response to filestream
                await response.Content.CopyToAsync(fileStream);
                System.Diagnostics.Process.Start(StrInvoiceNo.Replace("/", "-") + ".pdf");
            }
        }

Respostas

2 Hostel Sep 20 2020 at 01:13

asynce awaitforam introduzidos no C # 5 . Visual Studio 2010 oferece suporte a C # 4 . É por isso que você não pode compilar código com async/ awaitno VS 2010.