I am trying to load some files in my .NET MAUI application, I am using HttpClient inside my Application
constructor (I know that I should be using App lifecycle events) :
public partial class App : Application
{
public App()
{
InitializeComponent();
TestAsync();
}
private async Task TestAsync()
{
HttpClient lClient = new HttpClient();
var lReponse = await lClient.GetAsync(new Uri("https://proof.ovh.net/files/1Mb.dat"));
using (var fs = new FileStream(@"C:\test.dat", FileMode.CreateNew))
{
await lReponse.Content.CopyToAsync(fs);
}
}
}
I always end up with the following error on Windows (An unhandled win32 exception occurred) on the var lReponse = await lClient.GetAsync
part :
In a .NET 6 WPF project this is working fine :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TestAsync();
}
private async Task TestAsync()
{
HttpClient lClient = new HttpClient();
var lReponse = await lClient.GetAsync(new Uri("https://proof.ovh.net/files/1Mb.dat"));
using (var fs = new FileStream(@"C:\test.dat", FileMode.CreateNew))
{
await lReponse.Content.CopyToAsync(fs);
}
}
}
Is there something specific in the lifecycle of the Application
class that impact async/await (something related to the SynchronizationContext ?) ?
Thanks for your help !