0

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 :

enter image description here

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 !

6
  • likely your user does not have permission to write to the root of the C:\ drive Commented Jan 11, 2023 at 17:00
  • add a try/catch and get the exception object and see what it tells you Commented Jan 11, 2023 at 17:18
  • @daniel-a-white I tried with other paths (FileSystem.Current.AppDataDirectory), same result. Seems unlikely to me that it is a filesystem problem since the error is on lClient.GetAsync and it work in WPF. Commented Jan 12, 2023 at 8:02
  • @Jason no luck with that (same error, same place, catch never reach) nothing in the output as well Commented Jan 12, 2023 at 8:10
  • Please post the complete exception details, including message and call stack. And code, if you can get it. Commented Jan 12, 2023 at 10:58

1 Answer 1

2

The error was caused by the missing MainPage initalization. In .NET MAUI you have to set MainPage in the constructor of App, this will fix my code sample :

public App()
{
   InitializeComponent();
   TestAsync();

   MainPage = new AppShell();
}

I found out thanks to this stackoverflow, how you can handle this type of unhandled exception like so :

public App()
{
    // Catch all handled exceptions in managed code, before the runtime searches the Call Stack 
    AppDomain.CurrentDomain.FirstChanceException += FirstChanceException;
    InitializeComponent();
    TestAsync();
}

private void FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
    Console.WriteLine(e.ToString());
}

Which gave me the following exception :

System.NotImplementedException: 'Either set MainPage or override CreateWindow.'

In my original code I was setting the main page after the await of my async call, which is after the constructor finished it's execution hence the error (no idea it was hide by a win32 unhandled exception though).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.