2

I am converting my Xamarin Forms Application to #dotnetMAUI. In Xamarin I used dryloc for dependency injection. But since dependency injection was built in MAUI, I would like to use it. My question is, how can I configure a bunch of services and view models later into the container? My app is developed as a bunch of modules and initially, I just want to put the services and view models into the container that is required by the App's start-up page. SO that the start-up will be fast. In dryloc this was possible but I am not sure how to do it in the Maui app.

e.g.

I have two modules.

  1. Startup Module which contains only a startup page view model and its services.
  2. Chat Module which contains all the view models and services required for chat.

So, at first, I would like to configure the startup module in IOC container while creating the MAUI app.

And once the start-up page is open and the user clicks on something, I would like to put services and view models of the chat module into the IOC container.

In total, there are more than 200 services and view models in the app, and putting all those in the IOC container at the beginning might affect the start performance. In Xamarin Forms, It was the case and that is why I followed this approach.

Pravin Patil
  • 418
  • 5
  • 23
  • [I access it in App constructor, storing it into a static variable of App](https://stackoverflow.com/a/72439742/199364). `public static IServiceProvider Services;`, which is set in App constructor. – ToolmakerSteve Jun 09 '22 at 02:46
  • @ToolmakerSteve Actually, I want to inject services and view models into the container after the app is initialized and displayed. – Pravin Patil Jun 09 '22 at 02:51
  • IServiceProvider doesn't have AddSingleton and AddTransient methods. IServiceCollection has those methods. – Pravin Patil Jun 09 '22 at 02:55
  • Sorry, maybe my question is not clear. I will modify it. – Pravin Patil Jun 09 '22 at 02:59
  • 1
    Are you sure that moving the registration to be after startup would really make a meaningful performance difference? Sounds like pre-optimization to me. Instead, just get it working, measure how long it takes then decide if it's acceptable or not. – mason Jun 09 '22 at 03:01
  • @ToolmakerSteve I have modified my question and actually, my app gained significant performance improvement in Xamarin forms with that approach. – Pravin Patil Jun 09 '22 at 03:09

1 Answers1

1

Unfortunately, adding to the ServiceCollection is not allowed after application is built.

Proof:

// In MauiProgram.cs:
public static IServiceCollection Services;

public static MauiApp CreateMauiApp()
{
    ...
    Services = builder.Services;
    return builder.Build();
}

// In MainPage.xaml.cs / OnAppearing:
MauiProgram.Services.AddSingleton<TestClass>();

Result is Exception:

System.InvalidOperationException
  HResult=0x80131509  
  Message=Cannot modify ServiceCollection after application is built.
  Source=Microsoft.Maui  
  StackTrace:
   at Microsoft.Maui.Hosting.MauiApplicationServiceCollection.CheckServicesAccess()  
   at Microsoft.Maui.Hosting.MauiApplicationServiceCollection.Add(ServiceDescriptor item)
   ...

Maui is making use of Dependency injection in .NET.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196