No service for type 'Microsoft.Maui.IApplication' has been registered thrown on Android MAUI.NET

756 Views Asked by At

I have a MAUI.NET app that is running well on Windows. Now I want to give it a try for Android. I have plug in real local Android device with Android 11, set all projects to .NET 6, enabled developer mode and USB debugging... After all it compiled and run. But I a getting such an error shortly after startup:

System.InvalidOperationException: 'No service for type 'Microsoft.Maui.IApplication' has been registered.'

That is weird, because I do not use IApplication at all, explicitly myself. So I suppose something went wrong during some MAUI.NET inside code... Anyone can let some place where to look for? Google doesn't help in with this error.

I am able to debug app until line

return builder.Build();

at CreateMauiApp() method at MauiProgram class. And after F10 / F5 it get this error.

MainApplication constructor in myProject/Platforms/Android/MainApplication.cs is fired.

1

There are 1 best solutions below

1
0mpurdy On

I have just had this issue myself, our app was migrating from Prism 8 to Prism 9 at the same time and this used to be a Xamarin.Forms app which may be confounding factors but the bit I was missing was:

.UseMauiApp<App>()

The context of this is:

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();

        builder
            .UseMauiApp<App>()
            .UsePrism(prism =>
            {
                // ... etc
            });
#if ANDROID || IOS || WINDOWS || MACCATALYST
            .InitPlatform()
#endif
            // ... etc

        return builder.Build();
    }
}