Skip to content
4 changes: 2 additions & 2 deletions src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ static MauiAppBuilder SetupDefaults(this MauiAppBuilder builder)
});

#if WINDOWS
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IMauiInitializeService, MauiControlsInitializer>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IMauiInitializeScopedService, MauiControlsInitializer>());
#endif
builder.RemapForControls();

return builder;
}

class MauiControlsInitializer : IMauiInitializeService
class MauiControlsInitializer : IMauiInitializeScopedService
{
public void Initialize(IServiceProvider services)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Hosting/MauiApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ private void DisposeConfiguration()
(Configuration as IDisposable)?.Dispose();
}
}
}
}
6 changes: 3 additions & 3 deletions src/Core/src/Hosting/MauiAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ internal MauiAppBuilder(bool useDefaults)
this.UseEssentials();

#if WINDOWS
this.Services.TryAddEnumerable(ServiceDescriptor.Transient<IMauiInitializeService, MauiCoreInitializer>());
this.Services.TryAddEnumerable(ServiceDescriptor.Transient<IMauiInitializeScopedService, MauiCoreInitializer>());
#endif
}
}

class MauiCoreInitializer : IMauiInitializeService
class MauiCoreInitializer : IMauiInitializeScopedService
{
public void Initialize(IServiceProvider services)
{
Expand Down Expand Up @@ -160,7 +160,7 @@ public MauiApp Build()
{
foreach (var instance in initServices)
{
instance.Initialize(builtApplication.Services);
instance.Initialize(serviceProvider);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Core/src/MauiContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions src/Core/tests/DeviceTests.Shared/MauiProgramDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +70,12 @@ public static MauiApp CreateMauiApp(List<Assembly> testAssemblies)
#endif
appBuilder.UseVisualRunner();

appBuilder.ConfigureContainer(new DefaultServiceProviderFactory(new ServiceProviderOptions
{
ValidateOnBuild = true,
ValidateScopes = true,
}));

var mauiApp = appBuilder.Build();

DefaultTestApp = mauiApp.Services.GetRequiredService<IApplication>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down