Debug··.NET MAUIDependency Injection

No parameterless constructor defined for type MainPage on .NET MAUI

Got this exception when trying to inject a service on a ContentPage? This post will show you how to fix it in seconds.

Aptabase Team @aptabase

I spent hours trying to get a basic MAUI Hello World app to work with Dependency Injection and I couldn’t find any clear documentation on what was wrong with it, hence why I’m writing this post, hopefully it’ll help you spend less time debugging your MAUI app.

This is the exception I was getting:

System.MissingMethodException:
No parameterless constructor defined for type 'HelloWorld.MainPage'.

I’m familiar with AppBuilder concept, so the MauiApp.CreateBuilder felt very natural to me. I created a new project, registered some interfaces and classes on my MAUI dependency injection container using the builder.Services.AddSingleton<T> method and then I tried to inject my service on my MainPage constructor, something basic for ASP.NET Core apps, except you’d have a Controller instead of a ContentPage.

The MainPage looked like this:

public partial class MainPage : ContentPage
{
	IMyClient _client;
    int count = 0;

	public MainPage(IMyClient client)
	{
		InitializeComponent();
		_client = client;
	}
}

But when I tried to run the app, I got the following exception:

System.MissingMethodException:
No parameterless constructor defined for type 'HelloWorld.MainPage'.

The message is quite clear, it’s looking for a constructor without parameters, which I don’t have because I need that IMyClient to be injected.

It turns out that on a ASP.NET Core app, controllers are added to the dependency injection container using the AddControllers method, which you usually get by default from the template, so you probably don’t even notice it. But on a MAUI app, the ContentPage classes are NOT added to the dependency injection container by default, so I needed to add them myself.

The fix was a single-liner on my MauiProgram.cs after hours of troubleshooting and Googling 😅

builder.Services.AddSingleton<MainPage>();

If I had dozens or hundreds of page, I’d probably abstract that in a builder.Services.AddContentPages extension method, but for now, I’m fine with that.

I hope this helps you save some time debugging your MAUI app! If you have any questions or suggestions, feel free to reach out to me on Twitter, I’d love to hear from you!

Analytics for MAUI appsWithout compromising on privacy

Aptabase is a privacy-first analytics platform for .NET MAUI apps. Get insights into your app's usage in minutes.

Learn more
Aptabase Dashboard Screenshot

Want to learn more?

Asynchronous Events in .NET MAUI

Guide··System.Threading.Channels.NET MAUI

Asynchronous Events in .NET MAUI

Learn how to use System.Threading.Channels for efficient asynchronous event processing in your .NET MAUI applications.

Reading PDFs in .NET MAUI

Guide··PDFFilePicker.NET MAUI

Reading PDFs in .NET MAUI

Learn how to configure permissions, build a file-picker and handle PDF files in a cross-platform compatible way in .NET MAUI.

Resolving Javax.Net.Ssl.SSLHandshakeException

Guide··SSLAndroid.NET MAUI

Resolving Javax.Net.Ssl.SSLHandshakeException

Learn how to connect to localhost over SSL and resolve handshake errors like java.security.cert.CertPathValidatorException from an Android emulator in .NET MAUI.

Where would you prefer to host your data?

European UnionGermanyUnited StatesVirginia

We typically advise selecting the region nearest to the majority of your users' locations. However, if your user base is global and dispersed, opt for the region that is geographically closest to your own location.