From 27fbd27352024453f749c0dbd9960e1d1b8eb128 Mon Sep 17 00:00:00 2001 From: David Vreony Date: Mon, 14 Oct 2024 17:43:51 +0100 Subject: [PATCH] plug gap for update container for servicecollection --- .../MicrosoftDependencyResolver.cs | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Splat.Microsoft.Extensions.DependencyInjection/MicrosoftDependencyResolver.cs b/src/Splat.Microsoft.Extensions.DependencyInjection/MicrosoftDependencyResolver.cs index e36728d18..3ddad2b83 100644 --- a/src/Splat.Microsoft.Extensions.DependencyInjection/MicrosoftDependencyResolver.cs +++ b/src/Splat.Microsoft.Extensions.DependencyInjection/MicrosoftDependencyResolver.cs @@ -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,9 +99,15 @@ public void UpdateContainer(IServiceProvider serviceProvider) lock (_syncLock) { + // 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; - DisposeServiceProvider(_serviceProvider); - _serviceProvider = serviceProvider; _isImmutable = true; } }