'The return type of an async method must be void

I am currently following a course on how to build a ASP.NET blazor app and have reached a dead end. As the title states, the method CreateMauiApp() requires a void return type. One critical difference is that I am following a course that is 1 year old and does not cover Maui which I am using. Is there any work-around to this issue that anyone knows of?

 public static class MauiProgram
{
    public static async MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .RegisterBlazorMauiWebView()
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            });
        //builder.Services.AddSingleton<IUserManager, UserManager>();
        //builder.Services.AddSingleton<WeatherForecastService>();

        builder.Services.AddSingleton<IUserManager, UserManagerFake>();
        builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();

        builder.Services.AddBlazorWebView();


        var host = builder.Build();
        var currentUserService = host.Services.GetRequiredService<ICurrentUserService>();
        TestData.CreateTestUser();
        currentUserService.CurrentUser = TestData.TestUser;

        await host.RunAsync();
        
    }
}

}

Error messages:

Error CS0161 'MauiProgram.CreateMauiApp()': not all code paths return a value UdemyCourseIntro (net6.0-android), UdemyCourseIntro (net6.0-ios), UdemyCourseIntro (net6.0-maccatalyst), UdemyCourseIntro (net6.0-windows10.0.19041) C:\Users\matej\source\repos\UdemyCourseIntro\UdemyCourseIntro\MauiProgram.cs 11 Active

Error CS1983 The return type of an async method must be void, Task, Task, a task-like type, IAsyncEnumerable, or IAsyncEnumerator UdemyCourseIntro (net6.0-android), UdemyCourseIntro (net6.0-ios), UdemyCourseIntro (net6.0-maccatalyst), UdemyCourseIntro (net6.0-windows10.0.19041) C:\Users\matej\source\repos\UdemyCourseIntro\UdemyCourseIntro\MauiProgram.cs 11 Active



Solution 1:[1]

So the issue was exactly as someone politely stated in one of the comments. All I had to do is delete the async and return a MauiApp object.

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .RegisterBlazorMauiWebView()
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            });
        //builder.Services.AddSingleton<IUserManager, UserManager>();
        builder.Services.AddSingleton<IUserManager, UserManagerFake>();
        builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();

        builder.Services.AddBlazorWebView();

        //builder.Services.AddSingleton<WeatherForecastService>();

        var host = builder.Build();
        var currentUserService = host.Services.GetRequiredService<ICurrentUserService>();
        TestData.CreateTestUser();
        currentUserService.CurrentUser = TestData.TestUser;

        host.RunAsync();

        return host;
    }
}

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matej Hackl