A component of ASP.NET for creating RESTful web services that support HTTP-based communication between clients and servers.
DownloadAndShareFileAsync is declared as a public static method inside MauiProgram, but it is being called directly inside CreateMauiApp without await and without qualifying the call.
Key issues:
-
CreateMauiAppmust return aMauiAppsynchronously. It cannot be markedasyncand cannotawaitan async method. - A
Task-returning method likeDownloadAndShareFileAsyncshould not be called and ignored in a startup method such asCreateMauiApp; that will either not compile (ifawaitis used) or will compile with warnings and run at an unexpected time. - If the call is inside
CreateMauiAppbody, it must be fully qualified asMauiProgram.DownloadAndShareFileAsync(...)because it is a static method of the same class, but even then it still returns aTaskthat cannot be awaited there.
Recommended changes:
- Remove the call to
DownloadAndShareFileAsyncfromCreateMauiApp:
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
// configure builder...
return builder.Build();
}
public static async Task DownloadAndShareFileAsync(string fileUrl, string fileName)
{
// implementation
}
}
- Call
DownloadAndShareFileAsyncfrom UI code (for example, from a button click handler or command) whereasync/awaitis allowed:
private async void OnDownloadButtonClicked(object sender, EventArgs e)
{
await MauiProgram.DownloadAndShareFileAsync("https://...", "MyFile.txt");
}
This keeps CreateMauiApp purely for app configuration and avoids build errors related to calling an async method from a non-async factory method.
References: