0

I have some C# code that Im actually porting to VB.Net. Now I noticed ConfigureAwait(false) is used everywhere. A requirement is that the code be dependant on .Net 4.0

As you might know... ConfigureAwait(false) appears in .Net 4.5. How would I convert the following code to be compliant with .Net 4.0?

Is there a generic solution as ConfigureAwait occurs everywhere in the code

 public async Task<ListResponse> GetResponseAsync(ListRequest request = null)
        {
            request =  request ?? new ListRequest
            {
                Limit = _limit
            };

            using (var client = CreateMailClient("lists"))
            {
                var response = await client.GetAsync(request.ToQueryString()).ConfigureAwait(false);
                await response.EnsureSuccessMailChimpAsync().ConfigureAwait(false);

                var listResponse = await response.Content.ReadAsAsync<ListResponse>().ConfigureAwait(false);
                return listResponse;
            }
        }

ANSWER: Reference Microsoft.BCL.Async

Eminem
  • 7,206
  • 15
  • 53
  • 95
  • It seems like the code was peppered with `ConfigureAwait` *everywhere*. That seems excessive. Do you know if this has been done in general (in response to an ill-advised coding guideline) or are the `ConfigureAwait` really needed in all occasions? If not, you first have to identify the cases where they are really necessary. When I do async code I almost never need it. – Sefe Jul 10 '18 at 07:09
  • 1
    ConfigureAwait(false) is your least concert since async/await is introduced in .Net 4.5, so you'll need to use Task continuation instead – Darjan Bogdan Jul 10 '18 at 07:09
  • As Darjan said, that code cannot be ported to .NET 4 *as is*. `async/await` and `HttpClient` are supported from .NET 4.5 onwards, so you'll need to rewrite that part entirely. – Federico Dipuma Jul 10 '18 at 07:40
  • Apparantly I have to include Microsoft.BCL.Async :) – Eminem Jul 10 '18 at 08:04
  • 1
    @Eminem - async/await (and `HttpClient`) work fine with Microsoft.Bcl.Async on NET40, but note that target machines have to have [KB2468871](https://www.microsoft.com/en-gb/download/details.aspx?id=3556) installed to use it. If they're regularly updated they should have it, but it's caught me out before. – sellotape Jul 10 '18 at 11:02

1 Answers1

0

This is what you are looking for to be precise.

If you are using ConfigureAwait(), you probably really care about it and want it to actually work. Unfortunately, because async methods might actually complete synchronously, the call to ConfigureAwait() might not affect anything. That means you have to put it on the next async call too, and so on, until it is on every single method in your library.

Reference Microsoft Developers

Alternatives to Configure await

Sample code

DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43