diff --git a/src/Core/src/Hosting/Internal/MauiApplicationServiceCollection.cs b/src/Core/src/Hosting/Internal/MauiApplicationServiceCollection.cs deleted file mode 100644 index bba34f3a2a47..000000000000 --- a/src/Core/src/Hosting/Internal/MauiApplicationServiceCollection.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.Maui.Hosting -{ - internal sealed class MauiApplicationServiceCollection : IServiceCollection - { - private IServiceCollection _services = new ServiceCollection(); - - public ServiceDescriptor this[int index] - { - get => _services[index]; - set - { - CheckServicesAccess(); - _services[index] = value; - } - } - public int Count => _services.Count; - - public bool IsReadOnly { get; set; } - - public IServiceCollection InnerCollection - { - get => _services; - set - { - CheckServicesAccess(); - _services = value; - } - } - - public void Add(ServiceDescriptor item) - { - CheckServicesAccess(); - - _services.Add(item); - } - - public void Clear() - { - CheckServicesAccess(); - - _services.Clear(); - } - - public bool Contains(ServiceDescriptor item) - { - return _services.Contains(item); - } - - public void CopyTo(ServiceDescriptor[] array, int arrayIndex) - { - _services.CopyTo(array, arrayIndex); - } - - public IEnumerator GetEnumerator() - { - return _services.GetEnumerator(); - } - - public int IndexOf(ServiceDescriptor item) - { - return _services.IndexOf(item); - } - - public void Insert(int index, ServiceDescriptor item) - { - CheckServicesAccess(); - - _services.Insert(index, item); - } - - public bool Remove(ServiceDescriptor item) - { - CheckServicesAccess(); - - return _services.Remove(item); - } - - public void RemoveAt(int index) - { - CheckServicesAccess(); - - _services.RemoveAt(index); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - private void CheckServicesAccess() - { - if (IsReadOnly) - { - throw new InvalidOperationException("Cannot modify ServiceCollection after application is built."); - } - } - } -} diff --git a/src/Core/src/Hosting/MauiAppBuilder.cs b/src/Core/src/Hosting/MauiAppBuilder.cs index 81ecb42ddfb2..a6c83d8100e9 100644 --- a/src/Core/src/Hosting/MauiAppBuilder.cs +++ b/src/Core/src/Hosting/MauiAppBuilder.cs @@ -14,7 +14,7 @@ namespace Microsoft.Maui.Hosting /// public sealed class MauiAppBuilder { - private readonly MauiApplicationServiceCollection _services = new(); + private readonly ServiceCollection _services = new(); private Func? _createServiceProvider; private readonly Lazy _configuration; private ILoggingBuilder? _logging; @@ -152,7 +152,7 @@ public MauiApp Build() MauiApp builtApplication = new MauiApp(serviceProvider); // Mark the service collection as read-only to prevent future modifications - _services.IsReadOnly = true; + _services.MakeReadOnly(); var initServices = builtApplication.Services.GetServices(); if (initServices != null)