async non funziona con Visual Studio 2010 e .NET 4.0? [duplicare]

Sep 19 2020

Ho un codice C # sotto, funziona bene in Visual Studio 2017 con .Net Framework 4.0, ma quando eseguo lo stesso codice sorgente in Visual Studio 2010 con .net Framework, non funziona e mostra un messaggio di errore compilato vicino a async e attendere. Si prega di avvisare la soluzione alternativa di farlo.

Codice sorgente :-

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");
            }
        }

Risposte

2 Hostel Sep 20 2020 at 01:13

asynce awaitsono stati introdotti in C # 5 . Visual Studio 2010 supporta C # 4 . Ecco perché non è possibile compilare codice con async/ awaitsotto VS 2010.