Install any skill in seconds. Free to start, no credit card required.
Get Started Free →dotnet/maui-dependency-injection
Guidance for configuring dependency injection in .NET MAUI apps — service registration in MauiProgram.cs, lifetime selection (Singleton / Transient / Scoped), constructor injection, Shell navigation auto-resolution, platform-specific registrations, and testability patterns. USE FOR: "dependency injection", "DI setup", "AddSingleton", "AddTransient", "AddScoped", "service registration", "constructor injection", "IServiceProvider", "MauiProgram DI", "register services", "BindingContext injection".
Passed 22 of 23 test cases with the skill, and 22 of 23 without it.
1 fixed21 passed either way0 still failing1 broke
Tokens, turns and time describe resource use, not a dollar price.
A test case the skill fixed. Same model, same prompt, with and without the skill.
A .NET MAUI app navigates to `ProfilePage` using `Shell.Current.GoToAsync("ProfilePage?userId=42")`. How should the `ProfileViewModel` be written so it receives both its container dependency `IUserService` and the `userId` navigation parameter?
| Checked | Without skill | With skill |
|---|---|---|
| injects IUserService through the ProfileViewModel constructor | Passed | Passed |
| implements IQueryAttributable on ProfileViewModel | Missed | Passed |
| handles parameter extraction inside ApplyQueryAttributes | Missed | Passed |
The instructions your model reads when the skill loads.
.NET MAUI uses the same Microsoft.Extensions.DependencyInjection container as ASP.NET Core. All service registration happens in MauiProgram.CreateMauiApp() on builder.Services. The container is built once at startup and is immutable thereafter.
MauiProgram.csAddSingleton, AddTransient, and AddScoped#if directivesMauiProgram.cs file| Situation | Do this | Why |
|---|---|---|
| Registering a Page or ViewModel | Prefer AddTransient | A fresh instance per navigation avoids stale state, and a Singleton page cannot be re-added to the visual tree after it is removed. Singleton is defensible for a genuinely single-instance page (e.g. a root tab you want to keep warm) |
| Registering shared/expensive state | AddSingleton | One instance app-wide (settings, DB connection, HttpClient handler) |
Tempted to use AddScoped | Use AddTransient (or AddSingleton if sharing is intended) | MAUI has no built-in request scope like ASP.NET Core's HTTP pipeline. MAUI does create one IServiceScope per window, so a Scoped service lives as long as that window — and resolved from the root provider it behaves like a Singleton. Neither gives you per-navigation freshness |
| Navigating to a DI-registered page | Register the page and its ViewModel, then Routing.RegisterRoute | Shell.Current.GoToAsync resolves the page through DI and injects its constructor dependencies |
| Platform-specific implementation | #if per platform with every platform covered | A missing platform branch leaves the service unregistered and throws at resolution time |
Do not introduce DI into a project that isn't using it, swap a working service lifetime, or add an interface purely for symmetry — only when the user asked or it fixes a real defect.
Answer narrowly, but completely. When you recommend a lifetime change, show the
test cases passed, vs 22 / 23 without
Published skills whose descriptions closely match this one, with their headline benchmark lift.
More skills from the same source and nearby in the registry, with their headline benchmark lift.