maui-dependency-injection
What it does
This skill sets up dependency injection in .NET MAUI apps, where services are registered at startup and handed to the pages and view models that need them. It is for developers organizing services in a MAUI app.
When to use it
- ✓When you want to register a service, page or view model in a .NET MAUI app.
- ✓When you need to choose between singleton, transient and scoped lifetimes.
- ✓When you want a service to work with Shell navigation and be easy to test.
Skill link
https://github.com/dotnet/skills/blob/main/plugins/dotnet-maui/skills/maui-dependency-injection/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“Register my API client as a singleton and inject it into my MAUI view models.”
5 new skills a week. 1 minute to read.
How it works
The agent adds the registrations to the app's startup file, chooses a lifetime for each service, and wires constructor injection for pages and view models.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
Dependency Injection in .NET MAUI
.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.
When to Use
- Registering services, ViewModels, and Pages in
MauiProgram.cs - Choosing between
AddSingleton,AddTransient, andAddScoped - Wiring constructor injection for Pages and ViewModels
- Leveraging Shell navigation to auto-resolve DI-registered Pages
- Registering platform-specific service implementations with
#ifdirectives - Designing interfaces for testable service layers
When Not to Use
- XAML data-binding syntax or compiled bindings — use the maui-data-binding skill
- Shell route registration and query parameters — use the maui-shell-navigation skill
- Mocking frameworks or test runners — use standard .NET testing tools (xUnit, NUnit, MSTest) and mocking libraries (NSubstitute, Moq)
Inputs
- A .NET MAUI project with a
MauiProgram.csfile - Knowledge of which services, ViewModels, and Pages need registration
- Target platforms (Android, iOS, Mac Catalyst, Windows) for conditional registrations
Rules That Change the Answer
| 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
registration code, and give the realistic alternatives rather than a single verdict —
for a unit-of-work or DbContext question that means AddTransient, an explicit
IServiceScopeFactory.CreateScope(), and the factory pattern
(AddDbContextFactory), with a note on when each fits. A one-line prescription is
usually a worse answer than a short menu with trade-offs.
// Explicit scope when you genuinely need unit-of-work semantics
using var scope = scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<MyDbContext>();
Workflow
- Identify all services, ViewModels, and Pages that need to participate in dependency injection.
- Choose the correct lifetime for each type —
AddSingletonfor shared services,AddTransientfor Pages and ViewModels. - Register all types in
MauiProgram.CreateMauiApp()onbuilder.Services, grouping by category (services, HTTP, ViewModels, Pages). - Register Pages as Shell routes in
AppShell.xaml.csso Shell navigation auto-resolves the full dependency graph. - Wire each Page to its ViewModel via constructor injection, assigning the ViewModel as
BindingContext. - Add platform-specific registrations with
#ifdirectives, ensuring every target platform is covered or has a fallback. - Verify resolution works by running the app and confirming no
nulldependencies or missing-registration exceptions at runtime.
Lifetime Selection
| Lifetime | When to Use | Typical Types |
|---|---|---|
AddSingleton<T>() |
Shared state, expensive to create, app-wide config | HttpClient factory, settings service, database connection |
AddTransient<T>() |
Lightweight, stateless, or needs a fresh instance |
FAQ
Where are services registered?
Registrations go in the app's MauiProgram file, in the service collection.
What is a transient lifetime?
It gives a new instance each time the service is requested.
Does it cover mocking for tests?
It covers testable designs, but mocking tools are separate.
Details
Original description by the author
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". DO NOT USE FOR: data binding (use maui-data-binding), Shell route configuration (use maui-shell-nav
repo: dotnet/skills
Popularity
- GitHub stars
- 5.8k
- Ranking position
- #14586
- License
- MIT
- Updated
- 11/2/2026
Comments
- …
More skills from this repository
create-custom-agent
5.8kHelps your agent create VS Code custom agent files with their own tools, instructions and handoffs to other agents.
create-skill
5.8kHelps your agent create a new agent skill with a valid name, a clear trigger description and a SKILL.md file that follows the standard.
csharp-scripts
5.8kHelps your agent run a small C# program as a single file with the .NET command line tool, without creating a full project.
dotnet-pinvoke
5.8kHelps your agent call native C or C++ libraries from .NET correctly, covering signatures, string handling, memory and safe handles.
Similar skills
android-clean-architecture
280kMakes your agent organize Android projects efficiently. It structures the code to make maintenance and new feature development easier.
compose-multiplatform-patterns
280kHelps your agent use Compose Multiplatform and Jetpack Compose patterns in KMP projects, covering state, navigation, theming, and performance.
dart-flutter-patterns
280kMakes your agent write code for Dart and Flutter apps. It uses modern patterns to create robust and manageable apps.