diff --git a/src/Splat.Microsoft.Extensions.DependencyInjection/MicrosoftDependencyResolver.cs b/src/Splat.Microsoft.Extensions.DependencyInjection/MicrosoftDependencyResolver.cs
index 5e0533bf2..af734a3a7 100644
--- a/src/Splat.Microsoft.Extensions.DependencyInjection/MicrosoftDependencyResolver.cs
+++ b/src/Splat.Microsoft.Extensions.DependencyInjection/MicrosoftDependencyResolver.cs
@@ -12,7 +12,7 @@ namespace Splat.Microsoft.Extensions.DependencyInjection;
/// Microsoft DI implementation for .
///
///
-public class MicrosoftDependencyResolver : IDependencyResolver
+public class MicrosoftDependencyResolver : IDependencyResolver, IAsyncDisposable
{
private const string ImmutableExceptionMessage = "This container has already been built and cannot be modified.";
private readonly object _syncLock = new();
@@ -50,6 +50,38 @@ protected virtual IServiceProvider? ServiceProvider
}
}
+ ///
+ /// Updates this instance with a collection of configured services.
+ ///
+ /// An instance of .
+ public void UpdateContainer(IServiceCollection services)
+ {
+#if NETSTANDARD || NETFRAMEWORK
+ if (services is null)
+ {
+ throw new ArgumentNullException(nameof(services));
+ }
+#else
+ ArgumentNullException.ThrowIfNull(services);
+#endif
+
+ if (_isImmutable)
+ {
+ throw new InvalidOperationException(ImmutableExceptionMessage);
+ }
+
+ lock (_syncLock)
+ {
+ if (_serviceProvider is not null)
+ {
+ DisposeServiceProvider(_serviceProvider);
+ _serviceProvider = null;
+ }
+
+ _serviceCollection = services;
+ }
+ }
+
///
/// Updates this instance with a configured service Provider.
///
@@ -67,8 +99,15 @@ public void UpdateContainer(IServiceProvider serviceProvider)
lock (_syncLock)
{
- _serviceCollection = null;
+ // can be null if constructor using IServiceCollection was used.
+ // and no fetch of a service was called.
+ if (_serviceProvider is not null)
+ {
+ DisposeServiceProvider(_serviceProvider);
+ }
+
_serviceProvider = serviceProvider;
+ _serviceCollection = null;
_isImmutable = true;
}
}
@@ -144,6 +183,7 @@ public virtual void Register(Func