What does a Scoped Service Lifetime mean to MAUI #7671
Replies: 5 comments 3 replies
-
this does require some clarification in terms of MAUI and MAUI Blazor, what is the scope of a scoped service, where does it start and end? |
Beta Was this translation helpful? Give feedback.
-
Scoped services last for the lifetime of a page where they are resolved |
Beta Was this translation helpful? Give feedback.
-
The original question was for non-blazor MAUI. Any scoped services will hang around with the scope of a MAUI page. If you stay completely within a blazor MAUI, then yes - those instances will essentially be singleton. I don't know the answer for Blazor WASM. Most services I make are singleton. |
Beta Was this translation helpful? Give feedback.
-
From the official document, it's not providing any information about scoped serice. For some design purpose, I tried to use scoped service for AppShell, but it seems the scope is not populated to it's child (ContentTemplate). I take it as an design defect for "MAUI with Dependency Injection". |
Beta Was this translation helpful? Give feedback.
-
Scoped services can be useful in MAUI. For example if you have a page with lot's of ContentViews that use their own ViewModels and you need to share injected services between them, you can instantiate those views using this injected service:
All views instantiated this way will share the same instance of injected scoped services. All you have to do is to inject IServiceScopeFactory. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The
MauiAppBuilder.Services
property has a number of ways to register objects with dependency injection.AddTransient
will create a new instance of the object each time it is requested.AddSingleton
will return a single/reusable instance of an object.AddScoped
, in the context of ASP.NET will create a new instance per HTTP request. This paradigm doesn't exactly sync up with MAUI, with the exception of Blazor apps hosted in a MAUI container.Is there a defined use or meaning of a
Scoped
service in non-blazor MAUI?Beta Was this translation helpful? Give feedback.
All reactions