Remove calls to scoped services on root provider#19593
Remove calls to scoped services on root provider#19593
Conversation
| builder.Services.AddSingleton<ApplicationDispatcher>(); | ||
|
|
||
|
|
||
| builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IMauiInitializeService, DispatcherInitializer>()); |
There was a problem hiding this comment.
Is this ok @mattleibow ?
I can't really tell a difference here between using IMauiInitializeService vs IMauiInitializeScopedService. They are both fired from OnLaunched in MauiWinUIApplication so they both will capture the dispatcher from the main thread
The purpose of these are to initialize at the app level not the "Scoped" windows level so it seems like they should just be IMauiInitializeService
There was a problem hiding this comment.
IMauiInitializeService should only run once per app during the MauiAppBuilder.Build() method. The scoped services are supposed to be initialized for each window. However... I see that it is not working like that so I want to see what is really happening and if this is all a bug because we are not doing something...
|
/rebase |
94d95ca to
0e32a79
Compare
374e031 to
0c0431b
Compare
| return Dispatcher.GetForCurrentThread()!; | ||
| var dispatch = Dispatcher.GetForCurrentThread(); | ||
|
|
||
| return dispatch ?? svc.GetRequiredService<ApplicationDispatcher>().AppDispatcher; |
There was a problem hiding this comment.
I added the coalescing here, because we don't want to break users that are retrieving the scoped IDispatcher from the root service provider on a background thread. Because we are no longer saturating the IDispatcher on the root provider we need to account for this.
Description of Change
Alternative approach for #18492.
This approach opts for changing current behavior as little as possible for NET8. Instead of changing the behavior of
IMauiInitializeScopedServicethis just stops using it. AFAICT it's not really providing any current value for us. We can just useIMauiInitializeServiceto achieve the same workaround for WinUI.The main behavior change here is how to handle the first call to
IDispatcherfrom the service provider scoped to the window.Behavior Change
Currently in MAUI if you try to retrieve the
Dispatcherfrom the scoped service provider on a background thread it just returns null, but this captures the dispatcher when the window scope is created. Which is probably a better experience.https://github.com/dotnet/maui/pull/19593/files#diff-500f3422fdff10304272c2e7985ec98941543f788bef70ac1fb1b23776bb1104R49-R55
Added issue here to evaluate the correctness of
IMauiInitializeScopedServicebecause this interface currently only initializes once at the application level, it doesn't actually initialize for each scoped service we create.Issues Fixed
Fixes #11457