-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Remove calls to scoped services on root provider #19593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,19 +21,43 @@ public static MauiAppBuilder ConfigureDispatching(this MauiAppBuilder builder) | |
| if (DispatcherProvider.SetCurrent(provider)) | ||
| svc.CreateLogger<Dispatcher>()?.LogWarning("Replaced an existing DispatcherProvider with one from the service provider."); | ||
|
|
||
| return Dispatcher.GetForCurrentThread()!; | ||
| var dispatch = Dispatcher.GetForCurrentThread(); | ||
|
|
||
| return dispatch ?? svc.GetRequiredService<ApplicationDispatcher>().AppDispatcher; | ||
| }); | ||
| builder.Services.TryAddEnumerable(ServiceDescriptor.Scoped<IMauiInitializeScopedService, DispatcherInitializer>()); | ||
|
|
||
| builder.Services.TryAddSingleton<ApplicationDispatcher>(); | ||
|
|
||
|
|
||
| builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IMauiInitializeService, DispatcherInitializer>()); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this ok @mattleibow ? I can't really tell a difference here between using The purpose of these are to initialize at the app level not the "Scoped" windows level so it seems like they should just be
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMauiInitializeService should only run once per app during the |
||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| class DispatcherInitializer : IMauiInitializeScopedService | ||
| class DispatcherInitializer : IMauiInitializeService | ||
| { | ||
| public void Initialize(IServiceProvider services) | ||
| { | ||
| _ = services.GetRequiredService<IDispatcher>(); | ||
| _ = services.GetRequiredService<ApplicationDispatcher>(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// If the user tries to retrieve `IDispatcher` from the root `IServiceProvider` on a background thread | ||
| /// this serves as a way to retrieve the App level dispatcher | ||
| /// </summary> | ||
| internal class ApplicationDispatcher | ||
| { | ||
| public IDispatcher AppDispatcher { get; } | ||
|
|
||
| public ApplicationDispatcher() : this(Dispatcher.GetForCurrentThread()!) | ||
| { | ||
| } | ||
|
|
||
| internal ApplicationDispatcher(IDispatcher dispatcher) | ||
| { | ||
| AppDispatcher = dispatcher; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the coalescing here, because we don't want to break users that are retrieving the scoped
IDispatcherfrom the root service provider on a background thread. Because we are no longer saturating theIDispatcheron the root provider we need to account for this.