Skip to content

Commit

Permalink
plug gap for update container for servicecollection
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvreony committed Oct 14, 2024
1 parent 08a9104 commit 27fbd27
Showing 1 changed file with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ protected virtual IServiceProvider? ServiceProvider
}
}

/// <summary>
/// Updates this instance with a collection of configured services.
/// </summary>
/// <param name="services">An instance of <see cref="IServiceCollection"/>.</param>
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;
}
}

/// <summary>
/// Updates this instance with a configured service Provider.
/// </summary>
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 27fbd27

Please sign in to comment.