Did you know that the .NET compiler turns our async methods into classes? And that .NET adds a try/catch block to each of these classes, potentially hiding thrown exceptions? It's true!
2. @TheCodeTraveler https://codetraveler.io/AsyncAwaitBestPractices/
GetLibraries
public async Task<List<LibraryModel>> GetLibraries()
{
var response = await httpClient.GetAsync("https://codeTraveler.io/MauiLibraries");
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync();
var libraries = await JsonSerializer.DeserializeAsync<List<LibraryModel>>(stream);
return libraries ?? throw new InvalidOperationException("Libraries cannot be null");
}
3. @TheCodeTraveler https://codetraveler.io/AsyncAwaitBestPractices/
GetLibraries
public async Task<List<LibraryModel>> GetLibraries()
{
var response = await httpClient.GetAsync("https://codeTraveler.io/MauiLibraries");
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync();
var libraries = await JsonSerializer.DeserializeAsync<List<LibraryModel>>(stream);
return libraries ?? throw new InvalidOperationException("Libraries cannot be null");
}
Thread 1
4. @TheCodeTraveler https://codetraveler.io/AsyncAwaitBestPractices/
GetLibraries
public async Task<List<LibraryModel>> GetLibraries()
{
var response = await httpClient.GetAsync("https://codeTraveler.io/MauiLibraries");
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync();
var libraries = await JsonSerializer.DeserializeAsync<List<LibraryModel>>(stream);
return libraries ?? throw new InvalidOperationException("Libraries cannot be null");
}
Thread 2*
*Can be any thread other than Thread 1
e.g. Thread 32
5. @TheCodeTraveler https://codetraveler.io/AsyncAwaitBestPractices/
GetLibraries
public async Task<List<LibraryModel>> GetLibraries()
{
var response = await httpClient.GetAsync("https://codeTraveler.io/MauiLibraries");
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync();
var libraries = await JsonSerializer.DeserializeAsync<List<LibraryModel>>(stream);
return libraries ?? throw new InvalidOperationException("Libraries cannot be null");
}
Thread 1
6. @TheCodeTraveler https://codetraveler.io/AsyncAwaitBestPractices/
GetLibraries
public async Task<List<LibraryModel>> GetLibraries()
{
var response = await httpClient.GetAsync("https://codeTraveler.io/MauiLibraries");
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync();
var libraries = await JsonSerializer.DeserializeAsync<List<LibraryModel>>(stream);
return libraries ?? throw new InvalidOperationException("Libraries cannot be null");
}
Thread 5*
*Can be any thread other than Thread 1
e.g. Thread 32
7. @TheCodeTraveler https://codetraveler.io/AsyncAwaitBestPractices/
GetLibraries
public async Task<List<LibraryModel>> GetLibraries()
{
var response = await httpClient.GetAsync("https://codeTraveler.io/MauiLibraries");
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync();
var libraries = await JsonSerializer.DeserializeAsync<List<LibraryModel>>(stream);
return libraries ?? throw new InvalidOperationException("Libraries cannot be null");
}
Thread 1
8. @TheCodeTraveler https://codetraveler.io/AsyncAwaitBestPractices/
GetLibraries
public async Task<List<LibraryModel>> GetLibraries()
{
var response = await httpClient.GetAsync("https://codeTraveler.io/MauiLibraries");
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync();
var libraries = await JsonSerializer.DeserializeAsync<List<LibraryModel>>(stream);
return libraries ?? throw new InvalidOperationException("Libraries cannot be null");
}
Thread 17*
*Can be any thread other than Thread 1
e.g. Thread 32
9. @TheCodeTraveler https://codetraveler.io/AsyncAwaitBestPractices/
GetLibraries
public async Task<List<LibraryModel>> GetLibraries()
{
var response = await httpClient.GetAsync("https://codeTraveler.io/MauiLibraries");
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync();
var libraries = await JsonSerializer.DeserializeAsync<List<LibraryModel>>(stream);
return libraries ?? throw new InvalidOperationException("Libraries cannot be null");
}
Thread 1