Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"No parameterless constructor defined" error when attempting DI

I am trying to register a class for dependency inject, however when I tried to inject it through my constructor I get the following error:

System.MissingMethodException: ‘No parameterless constructor defined for type ‘MauiClient.MainPage’.’

I register my service in the mainprogram files as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

MainProgram.cs:

using Accounting.Shared.Services;
using Microsoft.Extensions.Logging;

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

            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });

            builder.Services.AddTransient<ITestService>(s =>
            {
                var httpClient = new HttpClient();
                var token = "";
                return new TestService(httpClient, token);
            });

            return builder.Build();
        }
    }
}

My actual service class:
TestService.cs:

namespace Accounting.Shared.Services;

public interface ITestService
{
}

public class TestService : ITestService
{
    private HttpClient _httpClient;
    private string _token;

    public TestService(HttpClient httpClient, string token)
    {        
        _httpClient = httpClient;
        _token = token;
    }
}

And finally in MainPage.xaml.cs I attempt the dependency injection:

namespace MauiClient;

using Accounting.Shared.Services;

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

    private ITestService _testService;

    public MainPage(ITestService testService)
    {
        InitializeComponent();
        _testService = testService; 
    }

    private void OnCounterClicked(object sender, EventArgs e)
    {
    }
}

I cannot see why it’s failing to pass it as a parameter. Any help would be appreciated.

>Solution :

You need to register not only the ITestService, but also the MainPage, when using it with <ShellContent>:

builder.Services.AddTransient<ITestService>(s =>
{
    var httpClient = new HttpClient();
    var token = "";
    return new TestService(httpClient, token);
});

builder.Services.AddTransient<MainPage>();

return builder.Build();

This is because Shell can only use parameterless constructors on its own.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading