1

I would like to use a service I created (with the interface IAuthenticationService ) inside the AppShell of a MAUI app (AppShell.xaml.cs). In other words to do something like this:

public partial class AppShell : Shell

private readonly IAuthenticationService _AuthenticationService;
public AppShell(AuthenticationService authenticationService)
{
    InitializeComponent();
    _AuthenticationService = authenticationService;
}

For the above I added the following code to the App.xaml.cs file

public partial class App : Application
{
    public App(AuthenticationService authenticationService)
    {
        InitializeComponent();
        MainPage = new AppShell(authenticationService);
    }
}

But when I run this, I get the error:

Unable to resolve service for type 'MyApp.Services.AuthenticationService' while attempting to activate 'MyApp.App'.'

Needless to say that I have done the code need in MauiProgram.cs

builder.Services.AddSingleton<IAuthenticationService, AuthenticationService>();

So basically how do I do DI in App.xaml.cs or in AppShell.xaml.cs?

Julian
  • 5,290
  • 1
  • 17
  • 40
adinas
  • 4,150
  • 3
  • 37
  • 47
  • Where in your app do you want to use the `IAuthenticationService`? The registration seems fine, but that way, it won't get passed into the App or AppShell automatically. Also, You should use `IAuthenticationService` instead of `AuthenticationService` in the constructor when using dependency injection, because you shouldn't be depending on the implementation (that's the whole point of DI). – Julian Mar 09 '23 at 15:08

2 Answers2

2

You're passing in the implementation instead of the interface, which is why the dependency cannot get resolved by the dependency container.

Your code should look like this instead:

private readonly IAuthenticationService _AuthenticationService;

//note the interface
public AppShell(IAuthenticationService authenticationService)
{
  InitializeComponent();
  _AuthenticationService = authenticationService;
}

and

public partial class App : Application
{
    //note the interface
    public App(IAuthenticationService authenticationService)
    {
      InitializeComponent();
      MainPage = new AppShell(authenticationService);
    }
}
Julian
  • 5,290
  • 1
  • 17
  • 40
  • Yes, You exactly found my issue. I need to use IAuthenticationService and not AuthenticationService. Thanks! – adinas Mar 09 '23 at 15:36
2

In MauiProgram.cs register:

builder.Services.AddSingleton<AppShell>();   
builder.Services.AddSingleton<IAuthenticationService,
AuthenticationService>();

In the App class:

public partial class App : Application   
{  
    public App(AppShell appShell)  
    {
        InitializeComponent();  
        MainPage = appShell;  
    }   
}

Finally in the AppShell class

public partial class AppShell : Shell                                 
{                                                                         
    private readonly IAuthenticationService _authenticationService;  
    public AppShell(IAuthenticationService authenticationService)                 
    {  
        InitializeComponent();   
        _authenticationService = authenticationService;                               
    }                                                                     
}
Julian
  • 5,290
  • 1
  • 17
  • 40
Paramjit
  • 760
  • 8
  • 25
  • I indeed needed to use "IAuthenticationService authenticationService" and not "AuthenticationService authenticationService" (I gave the other reply the answer because they replied first) but Thank you! – adinas Mar 09 '23 at 15:39