From aa20935c4a2d63cf3551b86f12deb98bc8a2e854 Mon Sep 17 00:00:00 2001 From: Mike Corsaro Date: Thu, 2 Nov 2023 12:56:51 -0700 Subject: [PATCH 01/13] Validate service provider scopes --- src/Core/src/Hosting/MauiAppBuilder.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Core/src/Hosting/MauiAppBuilder.cs b/src/Core/src/Hosting/MauiAppBuilder.cs index cdbc3ef72b88..a21de662e961 100644 --- a/src/Core/src/Hosting/MauiAppBuilder.cs +++ b/src/Core/src/Hosting/MauiAppBuilder.cs @@ -148,9 +148,11 @@ public MauiApp Build() IServiceProvider serviceProvider = _createServiceProvider != null ? _createServiceProvider() - : _services.BuildServiceProvider(); + : _services.BuildServiceProvider(validateScopes: true); - MauiApp builtApplication = new MauiApp(serviceProvider); + var appScope = serviceProvider.CreateScope(); + + MauiApp builtApplication = new MauiApp(appScope.ServiceProvider); // Mark the service collection as read-only to prevent future modifications _services.MakeReadOnly(); From 68ce8d711d6977f91a6670fcdc8267391080abbf Mon Sep 17 00:00:00 2001 From: Mike Corsaro Date: Thu, 2 Nov 2023 13:27:36 -0700 Subject: [PATCH 02/13] Remove validate :) --- src/Core/src/Hosting/MauiAppBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/src/Hosting/MauiAppBuilder.cs b/src/Core/src/Hosting/MauiAppBuilder.cs index a21de662e961..b6fdc7d9173a 100644 --- a/src/Core/src/Hosting/MauiAppBuilder.cs +++ b/src/Core/src/Hosting/MauiAppBuilder.cs @@ -148,7 +148,7 @@ public MauiApp Build() IServiceProvider serviceProvider = _createServiceProvider != null ? _createServiceProvider() - : _services.BuildServiceProvider(validateScopes: true); + : _services.BuildServiceProvider(); var appScope = serviceProvider.CreateScope(); From c6a11a60db1cc98f5ddb36abb2ec7182522fbdb3 Mon Sep 17 00:00:00 2001 From: Mike Corsaro Date: Thu, 2 Nov 2023 14:32:00 -0700 Subject: [PATCH 03/13] Add test-only scope validation --- .../DeviceTests.Shared/MauiProgramDefaults.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Core/tests/DeviceTests.Shared/MauiProgramDefaults.cs b/src/Core/tests/DeviceTests.Shared/MauiProgramDefaults.cs index b01d2202607f..4057fe612870 100644 --- a/src/Core/tests/DeviceTests.Shared/MauiProgramDefaults.cs +++ b/src/Core/tests/DeviceTests.Shared/MauiProgramDefaults.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Reflection; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Maui.Hosting; using Microsoft.Maui.LifecycleEvents; using Microsoft.Maui.TestUtils.DeviceTests.Runners; @@ -69,11 +70,29 @@ public static MauiApp CreateMauiApp(List testAssemblies) #endif appBuilder.UseVisualRunner(); + appBuilder.ConfigureContainer(new TestServiceCollection(appBuilder.Services)); + var mauiApp = appBuilder.Build(); DefaultTestApp = mauiApp.Services.GetRequiredService(); return mauiApp; } + + private class TestServiceCollection : IServiceProviderFactory + { + private IServiceCollection _services; + + public TestServiceCollection(IServiceCollection services) + { + _services = services; + } + + public ServiceCollection CreateBuilder(IServiceCollection services) + => new ServiceCollection { _services }; + + public IServiceProvider CreateServiceProvider(ServiceCollection containerBuilder) + => containerBuilder.BuildServiceProvider(validateScopes: true); + } } } \ No newline at end of file From 7f174408ae1753e5e0656247d3faad050387fed8 Mon Sep 17 00:00:00 2001 From: Mike Corsaro Date: Fri, 3 Nov 2023 15:07:56 -0700 Subject: [PATCH 04/13] Fix one test... --- .../UnitTests/Hosting/HostBuilderConfigureContainerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs b/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs index 04f0563f51e3..f8a507f60f63 100644 --- a/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs +++ b/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs @@ -14,7 +14,7 @@ public void CreatesServiceProviderByDefault() var builder = MauiApp.CreateBuilder(); var mauiApp = builder.Build(); - Assert.IsType(mauiApp.Services); + Assert.NotNull(mauiApp.Services); } [Fact] From 53e53b9ed0e53d1ecea68728308b1fc27f0cc6d2 Mon Sep 17 00:00:00 2001 From: Mike Corsaro Date: Wed, 8 Nov 2023 13:24:12 -0800 Subject: [PATCH 05/13] Fix root scope dispose Fix test --- src/Core/src/Hosting/MauiApp.cs | 7 ++++++- src/Core/src/Hosting/MauiAppBuilder.cs | 2 +- .../Hosting/HostBuilderConfigureContainerTests.cs | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Core/src/Hosting/MauiApp.cs b/src/Core/src/Hosting/MauiApp.cs index ca1df7d21230..25db484982a0 100644 --- a/src/Core/src/Hosting/MauiApp.cs +++ b/src/Core/src/Hosting/MauiApp.cs @@ -11,12 +11,16 @@ namespace Microsoft.Maui.Hosting public sealed class MauiApp : IDisposable, IAsyncDisposable { private readonly IServiceProvider _services; + private readonly IServiceProvider _rootServices; - internal MauiApp(IServiceProvider services) + internal MauiApp(IServiceProvider services, IServiceProvider rootServiceProvider) { _services = services; + _rootServices = rootServiceProvider; } + internal IServiceProvider RootServices => _rootServices; + /// /// The application's configured services. /// @@ -40,6 +44,7 @@ public void Dispose() DisposeConfiguration(); (_services as IDisposable)?.Dispose(); + (_rootServices as IDisposable)?.Dispose(); } /// diff --git a/src/Core/src/Hosting/MauiAppBuilder.cs b/src/Core/src/Hosting/MauiAppBuilder.cs index b6fdc7d9173a..5bca3476542a 100644 --- a/src/Core/src/Hosting/MauiAppBuilder.cs +++ b/src/Core/src/Hosting/MauiAppBuilder.cs @@ -152,7 +152,7 @@ public MauiApp Build() var appScope = serviceProvider.CreateScope(); - MauiApp builtApplication = new MauiApp(appScope.ServiceProvider); + MauiApp builtApplication = new MauiApp(appScope.ServiceProvider, serviceProvider); // Mark the service collection as read-only to prevent future modifications _services.MakeReadOnly(); diff --git a/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs b/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs index f8a507f60f63..e97c7cf9553b 100644 --- a/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs +++ b/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs @@ -34,8 +34,8 @@ public void ConfigureContainerCanReplaceIServiceProvider() builder => builder.Configured = true); var mauiApp = builder.Build(); - Assert.IsType(mauiApp.Services); - Assert.True(((MyServiceProvider)mauiApp.Services).Builder.Configured); + Assert.IsType(mauiApp.RootServices); + Assert.True(((MyServiceProvider)mauiApp.RootServices).Builder.Configured); } private class MyServiceProviderFactory : IServiceProviderFactory From 31c7a8360af0f0d2c3bdc8edd1b18edfef857be4 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 30 Nov 2023 16:44:13 -0600 Subject: [PATCH 06/13] - don't scope root container --- src/Core/src/Hosting/MauiApp.cs | 14 ++++++++++-- src/Core/src/Hosting/MauiAppBuilder.cs | 2 +- .../src/Platform/Android/MauiApplication.cs | 2 +- .../Platform/Windows/MauiWinUIApplication.cs | 2 +- .../Platform/iOS/MauiUIApplicationDelegate.cs | 2 +- .../DeviceTests.Shared/MauiProgramDefaults.cs | 22 +++++-------------- .../HostBuilderConfigureContainerTests.cs | 4 ++-- 7 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/Core/src/Hosting/MauiApp.cs b/src/Core/src/Hosting/MauiApp.cs index 25db484982a0..b07f652d8a9b 100644 --- a/src/Core/src/Hosting/MauiApp.cs +++ b/src/Core/src/Hosting/MauiApp.cs @@ -19,12 +19,12 @@ internal MauiApp(IServiceProvider services, IServiceProvider rootServiceProvider _rootServices = rootServiceProvider; } - internal IServiceProvider RootServices => _rootServices; + internal IServiceProvider ScopedServices => _services; /// /// The application's configured services. /// - public IServiceProvider Services => _services; + public IServiceProvider Services => _rootServices; /// /// The application's configured . @@ -61,6 +61,16 @@ public async ValueTask DisposeAsync() { (_services as IDisposable)?.Dispose(); } + + if (_rootServices is IAsyncDisposable rootAsyncDisposable) + { + // Fire and forget because this is called from a sync context + await rootAsyncDisposable.DisposeAsync(); + } + else + { + (_rootServices as IDisposable)?.Dispose(); + } } private void DisposeConfiguration() diff --git a/src/Core/src/Hosting/MauiAppBuilder.cs b/src/Core/src/Hosting/MauiAppBuilder.cs index 5bca3476542a..3761d32f6973 100644 --- a/src/Core/src/Hosting/MauiAppBuilder.cs +++ b/src/Core/src/Hosting/MauiAppBuilder.cs @@ -162,7 +162,7 @@ public MauiApp Build() { foreach (var instance in initServices) { - instance.Initialize(builtApplication.Services); + instance.Initialize(appScope.ServiceProvider); } } diff --git a/src/Core/src/Platform/Android/MauiApplication.cs b/src/Core/src/Platform/Android/MauiApplication.cs index 2f33449de75e..884a0491a104 100644 --- a/src/Core/src/Platform/Android/MauiApplication.cs +++ b/src/Core/src/Platform/Android/MauiApplication.cs @@ -35,7 +35,7 @@ public override void OnCreate() var mauiApp = CreateMauiApp(); - var rootContext = new MauiContext(mauiApp.Services, this); + var rootContext = new MauiContext(mauiApp.ScopedServices, this); var applicationContext = rootContext.MakeApplicationScope(this); diff --git a/src/Core/src/Platform/Windows/MauiWinUIApplication.cs b/src/Core/src/Platform/Windows/MauiWinUIApplication.cs index ce39fd4bd2d5..31d953dac65e 100644 --- a/src/Core/src/Platform/Windows/MauiWinUIApplication.cs +++ b/src/Core/src/Platform/Windows/MauiWinUIApplication.cs @@ -31,7 +31,7 @@ protected override void OnLaunched(UI.Xaml.LaunchActivatedEventArgs args) IPlatformApplication.Current = this; var mauiApp = CreateMauiApp(); - var rootContext = new MauiContext(mauiApp.Services); + var rootContext = new MauiContext(mauiApp.ScopedServices); var applicationContext = rootContext.MakeApplicationScope(this); diff --git a/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs b/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs index a25ae1509e1e..824b5009ea0c 100644 --- a/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs +++ b/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs @@ -44,7 +44,7 @@ public virtual bool WillFinishLaunching(UIApplication application, NSDictionary { var mauiApp = CreateMauiApp(); - var rootContext = new MauiContext(mauiApp.Services); + var rootContext = new MauiContext(mauiApp.ScopedServices); _applicationContext = rootContext.MakeApplicationScope(this); diff --git a/src/Core/tests/DeviceTests.Shared/MauiProgramDefaults.cs b/src/Core/tests/DeviceTests.Shared/MauiProgramDefaults.cs index 4057fe612870..7eef60b33791 100644 --- a/src/Core/tests/DeviceTests.Shared/MauiProgramDefaults.cs +++ b/src/Core/tests/DeviceTests.Shared/MauiProgramDefaults.cs @@ -70,7 +70,11 @@ public static MauiApp CreateMauiApp(List testAssemblies) #endif appBuilder.UseVisualRunner(); - appBuilder.ConfigureContainer(new TestServiceCollection(appBuilder.Services)); + appBuilder.ConfigureContainer(new DefaultServiceProviderFactory(new ServiceProviderOptions + { + ValidateOnBuild = true, + ValidateScopes = true, + })); var mauiApp = appBuilder.Build(); @@ -78,21 +82,5 @@ public static MauiApp CreateMauiApp(List testAssemblies) return mauiApp; } - - private class TestServiceCollection : IServiceProviderFactory - { - private IServiceCollection _services; - - public TestServiceCollection(IServiceCollection services) - { - _services = services; - } - - public ServiceCollection CreateBuilder(IServiceCollection services) - => new ServiceCollection { _services }; - - public IServiceProvider CreateServiceProvider(ServiceCollection containerBuilder) - => containerBuilder.BuildServiceProvider(validateScopes: true); - } } } \ No newline at end of file diff --git a/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs b/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs index e97c7cf9553b..f8a507f60f63 100644 --- a/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs +++ b/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs @@ -34,8 +34,8 @@ public void ConfigureContainerCanReplaceIServiceProvider() builder => builder.Configured = true); var mauiApp = builder.Build(); - Assert.IsType(mauiApp.RootServices); - Assert.True(((MyServiceProvider)mauiApp.RootServices).Builder.Configured); + Assert.IsType(mauiApp.Services); + Assert.True(((MyServiceProvider)mauiApp.Services).Builder.Configured); } private class MyServiceProviderFactory : IServiceProviderFactory From d79a6b4e3ebf30cd51fb34807a2271ab8c9bd2c2 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 30 Nov 2023 17:20:01 -0600 Subject: [PATCH 07/13] - cleanup --- src/Core/src/Hosting/MauiApp.cs | 39 ++++++++++++++------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/Core/src/Hosting/MauiApp.cs b/src/Core/src/Hosting/MauiApp.cs index b07f652d8a9b..e6eb510c5f12 100644 --- a/src/Core/src/Hosting/MauiApp.cs +++ b/src/Core/src/Hosting/MauiApp.cs @@ -10,16 +10,16 @@ namespace Microsoft.Maui.Hosting /// public sealed class MauiApp : IDisposable, IAsyncDisposable { - private readonly IServiceProvider _services; + private readonly IServiceProvider _applicationScopedServices; private readonly IServiceProvider _rootServices; internal MauiApp(IServiceProvider services, IServiceProvider rootServiceProvider) { - _services = services; + _applicationScopedServices = services; _rootServices = rootServiceProvider; } - internal IServiceProvider ScopedServices => _services; + internal IServiceProvider ScopedServices => _applicationScopedServices; /// /// The application's configured services. @@ -29,7 +29,7 @@ internal MauiApp(IServiceProvider services, IServiceProvider rootServiceProvider /// /// The application's configured . /// - public IConfiguration Configuration => _services.GetRequiredService(); + public IConfiguration Configuration => _applicationScopedServices.GetRequiredService(); /// /// Initializes a new instance of the class with optional defaults. @@ -43,7 +43,7 @@ public void Dispose() { DisposeConfiguration(); - (_services as IDisposable)?.Dispose(); + (_applicationScopedServices as IDisposable)?.Dispose(); (_rootServices as IDisposable)?.Dispose(); } @@ -51,25 +51,20 @@ public void Dispose() public async ValueTask DisposeAsync() { DisposeConfiguration(); + await DisposeService(_applicationScopedServices); + await DisposeService(_rootServices); - if (_services is IAsyncDisposable asyncDisposable) + async ValueTask DisposeService(IServiceProvider serviceProvider) { - // Fire and forget because this is called from a sync context - await asyncDisposable.DisposeAsync(); - } - else - { - (_services as IDisposable)?.Dispose(); - } - - if (_rootServices is IAsyncDisposable rootAsyncDisposable) - { - // Fire and forget because this is called from a sync context - await rootAsyncDisposable.DisposeAsync(); - } - else - { - (_rootServices as IDisposable)?.Dispose(); + if (serviceProvider is IAsyncDisposable rootAsyncDisposable) + { + // Fire and forget because this is called from a sync context + await rootAsyncDisposable.DisposeAsync(); + } + else + { + (serviceProvider as IDisposable)?.Dispose(); + } } } From a54d62c952daa5acdf19e44aca8abdf6f8e0ed15 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 30 Nov 2023 17:22:46 -0600 Subject: [PATCH 08/13] Update MauiApp.cs --- src/Core/src/Hosting/MauiApp.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Core/src/Hosting/MauiApp.cs b/src/Core/src/Hosting/MauiApp.cs index e6eb510c5f12..50934e3ac1f9 100644 --- a/src/Core/src/Hosting/MauiApp.cs +++ b/src/Core/src/Hosting/MauiApp.cs @@ -56,10 +56,10 @@ public async ValueTask DisposeAsync() async ValueTask DisposeService(IServiceProvider serviceProvider) { - if (serviceProvider is IAsyncDisposable rootAsyncDisposable) + if (serviceProvider is IAsyncDisposable asyncDisposable) { // Fire and forget because this is called from a sync context - await rootAsyncDisposable.DisposeAsync(); + await asyncDisposable.DisposeAsync(); } else { From 43184042e6eca389444fb0e543b2849dacf572ec Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Wed, 6 Dec 2023 11:36:21 -0700 Subject: [PATCH 09/13] - change configuration back to root services --- src/Core/src/Hosting/MauiApp.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/src/Hosting/MauiApp.cs b/src/Core/src/Hosting/MauiApp.cs index 50934e3ac1f9..09041bf47450 100644 --- a/src/Core/src/Hosting/MauiApp.cs +++ b/src/Core/src/Hosting/MauiApp.cs @@ -29,7 +29,7 @@ internal MauiApp(IServiceProvider services, IServiceProvider rootServiceProvider /// /// The application's configured . /// - public IConfiguration Configuration => _applicationScopedServices.GetRequiredService(); + public IConfiguration Configuration => _rootServices.GetRequiredService(); /// /// Initializes a new instance of the class with optional defaults. From c449fe97daffab965f951c4cb9ea7a48fb813223 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Wed, 6 Dec 2023 19:16:46 -0600 Subject: [PATCH 10/13] - pass in root service provider to IMauiInitializeService --- src/Core/src/Hosting/MauiAppBuilder.cs | 2 +- .../UnitTests/Hosting/HostBuilderConfigureContainerTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Core/src/Hosting/MauiAppBuilder.cs b/src/Core/src/Hosting/MauiAppBuilder.cs index 3761d32f6973..dd67ea6a7009 100644 --- a/src/Core/src/Hosting/MauiAppBuilder.cs +++ b/src/Core/src/Hosting/MauiAppBuilder.cs @@ -162,7 +162,7 @@ public MauiApp Build() { foreach (var instance in initServices) { - instance.Initialize(appScope.ServiceProvider); + instance.Initialize(serviceProvider); } } diff --git a/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs b/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs index f8a507f60f63..04f0563f51e3 100644 --- a/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs +++ b/src/Core/tests/UnitTests/Hosting/HostBuilderConfigureContainerTests.cs @@ -14,7 +14,7 @@ public void CreatesServiceProviderByDefault() var builder = MauiApp.CreateBuilder(); var mauiApp = builder.Build(); - Assert.NotNull(mauiApp.Services); + Assert.IsType(mauiApp.Services); } [Fact] From eeab76b0d5e09bbda01782e2444799f145b90124 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Wed, 6 Dec 2023 20:53:53 -0600 Subject: [PATCH 11/13] - change dispatcher initializers --- src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs | 4 ++-- src/Core/src/Hosting/MauiAppBuilder.cs | 4 ++-- src/Core/src/Platform/Tizen/MauiApplication.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs b/src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs index 9dd5733c06da..eec454b5f55f 100644 --- a/src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs +++ b/src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs @@ -184,14 +184,14 @@ static MauiAppBuilder SetupDefaults(this MauiAppBuilder builder) }); #if WINDOWS - builder.Services.TryAddEnumerable(ServiceDescriptor.Transient()); + builder.Services.TryAddEnumerable(ServiceDescriptor.Transient()); #endif builder.RemapForControls(); return builder; } - class MauiControlsInitializer : IMauiInitializeService + class MauiControlsInitializer : IMauiInitializeScopedService { public void Initialize(IServiceProvider services) { diff --git a/src/Core/src/Hosting/MauiAppBuilder.cs b/src/Core/src/Hosting/MauiAppBuilder.cs index dd67ea6a7009..624aa2a21ee6 100644 --- a/src/Core/src/Hosting/MauiAppBuilder.cs +++ b/src/Core/src/Hosting/MauiAppBuilder.cs @@ -43,12 +43,12 @@ internal MauiAppBuilder(bool useDefaults) this.UseEssentials(); #if WINDOWS - this.Services.TryAddEnumerable(ServiceDescriptor.Transient()); + this.Services.TryAddEnumerable(ServiceDescriptor.Transient()); #endif } } - class MauiCoreInitializer : IMauiInitializeService + class MauiCoreInitializer : IMauiInitializeScopedService { public void Initialize(IServiceProvider services) { diff --git a/src/Core/src/Platform/Tizen/MauiApplication.cs b/src/Core/src/Platform/Tizen/MauiApplication.cs index 96a57529834a..0adfc13dfefd 100644 --- a/src/Core/src/Platform/Tizen/MauiApplication.cs +++ b/src/Core/src/Platform/Tizen/MauiApplication.cs @@ -40,7 +40,7 @@ protected override void OnPreCreate() FontClient.Instance.AddCustomFontDirectory(fontResourcePath); var mauiApp = CreateMauiApp(); - var rootContext = new MauiContext(mauiApp.Services); + var rootContext = new MauiContext(mauiApp.ScopedServices); var platformWindow = CoreAppExtensions.GetDefaultWindow(); platformWindow.Initialize(); From 6fcde0dfab46a50fc34410a8c644377082b0111f Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Wed, 6 Dec 2023 21:13:02 -0600 Subject: [PATCH 12/13] - reorganize --- src/Core/src/Hosting/MauiApp.cs | 38 +++++++------------ src/Core/src/Hosting/MauiAppBuilder.cs | 4 +- src/Core/src/MauiContextExtensions.cs | 4 +- .../src/Platform/Android/MauiApplication.cs | 2 +- .../src/Platform/Tizen/MauiApplication.cs | 2 +- .../Platform/Windows/MauiWinUIApplication.cs | 2 +- .../Platform/iOS/MauiUIApplicationDelegate.cs | 2 +- 7 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/Core/src/Hosting/MauiApp.cs b/src/Core/src/Hosting/MauiApp.cs index 09041bf47450..2eb097546605 100644 --- a/src/Core/src/Hosting/MauiApp.cs +++ b/src/Core/src/Hosting/MauiApp.cs @@ -10,26 +10,22 @@ namespace Microsoft.Maui.Hosting /// public sealed class MauiApp : IDisposable, IAsyncDisposable { - private readonly IServiceProvider _applicationScopedServices; - private readonly IServiceProvider _rootServices; + private readonly IServiceProvider _services; - internal MauiApp(IServiceProvider services, IServiceProvider rootServiceProvider) + internal MauiApp(IServiceProvider services) { - _applicationScopedServices = services; - _rootServices = rootServiceProvider; + _services = services; } - internal IServiceProvider ScopedServices => _applicationScopedServices; - /// /// The application's configured services. /// - public IServiceProvider Services => _rootServices; + public IServiceProvider Services => _services; /// /// The application's configured . /// - public IConfiguration Configuration => _rootServices.GetRequiredService(); + public IConfiguration Configuration => _services.GetRequiredService(); /// /// Initializes a new instance of the class with optional defaults. @@ -43,28 +39,22 @@ public void Dispose() { DisposeConfiguration(); - (_applicationScopedServices as IDisposable)?.Dispose(); - (_rootServices as IDisposable)?.Dispose(); + (_services as IDisposable)?.Dispose(); } /// public async ValueTask DisposeAsync() { DisposeConfiguration(); - await DisposeService(_applicationScopedServices); - await DisposeService(_rootServices); - async ValueTask DisposeService(IServiceProvider serviceProvider) + if (_services is IAsyncDisposable asyncDisposable) + { + // Fire and forget because this is called from a sync context + await asyncDisposable.DisposeAsync(); + } + else { - if (serviceProvider is IAsyncDisposable asyncDisposable) - { - // Fire and forget because this is called from a sync context - await asyncDisposable.DisposeAsync(); - } - else - { - (serviceProvider as IDisposable)?.Dispose(); - } + (_services as IDisposable)?.Dispose(); } } @@ -75,4 +65,4 @@ private void DisposeConfiguration() (Configuration as IDisposable)?.Dispose(); } } -} +} \ No newline at end of file diff --git a/src/Core/src/Hosting/MauiAppBuilder.cs b/src/Core/src/Hosting/MauiAppBuilder.cs index 624aa2a21ee6..fb466ddbd1d4 100644 --- a/src/Core/src/Hosting/MauiAppBuilder.cs +++ b/src/Core/src/Hosting/MauiAppBuilder.cs @@ -150,9 +150,7 @@ public MauiApp Build() ? _createServiceProvider() : _services.BuildServiceProvider(); - var appScope = serviceProvider.CreateScope(); - - MauiApp builtApplication = new MauiApp(appScope.ServiceProvider, serviceProvider); + MauiApp builtApplication = new MauiApp(serviceProvider); // Mark the service collection as read-only to prevent future modifications _services.MakeReadOnly(); diff --git a/src/Core/src/MauiContextExtensions.cs b/src/Core/src/MauiContextExtensions.cs index 7bcf4ad192ab..186007ab02e2 100644 --- a/src/Core/src/MauiContextExtensions.cs +++ b/src/Core/src/MauiContextExtensions.cs @@ -37,7 +37,9 @@ public static IDispatcher GetDispatcher(this IMauiContext mauiContext) => public static IMauiContext MakeApplicationScope(this IMauiContext mauiContext, NativeApplication platformApplication) { - var scopedContext = new MauiContext(mauiContext.Services); + var scope = mauiContext.Services.CreateScope(); + + var scopedContext = new MauiContext(scope.ServiceProvider); scopedContext.AddSpecific(platformApplication); diff --git a/src/Core/src/Platform/Android/MauiApplication.cs b/src/Core/src/Platform/Android/MauiApplication.cs index 884a0491a104..2f33449de75e 100644 --- a/src/Core/src/Platform/Android/MauiApplication.cs +++ b/src/Core/src/Platform/Android/MauiApplication.cs @@ -35,7 +35,7 @@ public override void OnCreate() var mauiApp = CreateMauiApp(); - var rootContext = new MauiContext(mauiApp.ScopedServices, this); + var rootContext = new MauiContext(mauiApp.Services, this); var applicationContext = rootContext.MakeApplicationScope(this); diff --git a/src/Core/src/Platform/Tizen/MauiApplication.cs b/src/Core/src/Platform/Tizen/MauiApplication.cs index 0adfc13dfefd..96a57529834a 100644 --- a/src/Core/src/Platform/Tizen/MauiApplication.cs +++ b/src/Core/src/Platform/Tizen/MauiApplication.cs @@ -40,7 +40,7 @@ protected override void OnPreCreate() FontClient.Instance.AddCustomFontDirectory(fontResourcePath); var mauiApp = CreateMauiApp(); - var rootContext = new MauiContext(mauiApp.ScopedServices); + var rootContext = new MauiContext(mauiApp.Services); var platformWindow = CoreAppExtensions.GetDefaultWindow(); platformWindow.Initialize(); diff --git a/src/Core/src/Platform/Windows/MauiWinUIApplication.cs b/src/Core/src/Platform/Windows/MauiWinUIApplication.cs index 31d953dac65e..ce39fd4bd2d5 100644 --- a/src/Core/src/Platform/Windows/MauiWinUIApplication.cs +++ b/src/Core/src/Platform/Windows/MauiWinUIApplication.cs @@ -31,7 +31,7 @@ protected override void OnLaunched(UI.Xaml.LaunchActivatedEventArgs args) IPlatformApplication.Current = this; var mauiApp = CreateMauiApp(); - var rootContext = new MauiContext(mauiApp.ScopedServices); + var rootContext = new MauiContext(mauiApp.Services); var applicationContext = rootContext.MakeApplicationScope(this); diff --git a/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs b/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs index 824b5009ea0c..a25ae1509e1e 100644 --- a/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs +++ b/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs @@ -44,7 +44,7 @@ public virtual bool WillFinishLaunching(UIApplication application, NSDictionary { var mauiApp = CreateMauiApp(); - var rootContext = new MauiContext(mauiApp.ScopedServices); + var rootContext = new MauiContext(mauiApp.Services); _applicationContext = rootContext.MakeApplicationScope(this); From 8e38f71f6ca72df222caf8358bad0d03eae53363 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Sat, 23 Dec 2023 12:12:25 -0600 Subject: [PATCH 13/13] - scope app level test context --- .../HeadlessRunner/iOS/MauiTestApplicationDelegate.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/TestUtils/src/DeviceTests.Runners/HeadlessRunner/iOS/MauiTestApplicationDelegate.cs b/src/TestUtils/src/DeviceTests.Runners/HeadlessRunner/iOS/MauiTestApplicationDelegate.cs index cb83f8a7491e..1e66d5778e13 100644 --- a/src/TestUtils/src/DeviceTests.Runners/HeadlessRunner/iOS/MauiTestApplicationDelegate.cs +++ b/src/TestUtils/src/DeviceTests.Runners/HeadlessRunner/iOS/MauiTestApplicationDelegate.cs @@ -100,7 +100,11 @@ public override bool WillFinishLaunching(UIApplication application, NSDictionary }; var mauiApp = CreateMauiApp(); - Services = mauiApp.Services; + var rootContext = new MauiContext(mauiApp.Services); + + var appContext = rootContext.MakeApplicationScope(this); + + Services = appContext.Services; SetEnvironmentVariables();