4

In the startup.cs, I want to define an extended ServiceProvider: It basically wraps the default IServiceProvider implementation.

public IServiceProvider ConfigureServices(IServiceCollection services)
{
   [...]
   var servicesProvider = services.BuildExtendedServiceProvider();
   return servicesProvider;
}

Here is the core extended service provider implementation

/// <summary>
/// Extends the native asp.net service provider 
/// </summary>
public class ExtendedServicesProvider : IServiceProvider
{
    private readonly IServiceProvider _serviceProvider;

    /// <summary>
    /// Creates a new instance of <see cref="ExtendedServicesProvider"/> provider based on the native mvc <see cref="IServiceProvider"/>
    /// </summary>
    /// <param name="serviceProvider"></param>
    public ExtendedServicesProvider(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    /// <inheritDoc />
    public object GetService(Type serviceType)
    {
        var resolvedService = _serviceProvider.GetService(serviceType);
        [...]
        return resolvedService;
    }
}

At startup, I can see that GetService is called for each service with "singleton" life time (great!!) However, it is not called anymore after that, and the default ServiceProvider is called instead... By the way, if I request the resolution of IServiceProvider, this is the native one that I get.

I would like my serviceProvider to replace completely the native one and to be called EVERY TIME.

Did I miss something??

Tseng
  • 61,549
  • 15
  • 193
  • 205
Charles HETIER
  • 1,934
  • 16
  • 28

1 Answers1

0

Ok, I just checked the source code (I should have checked before posting the question sorry). https://github.com/aspnet/DependencyInjection/blob/master/src/Microsoft.Extensions.DependencyInjection/ServiceProvider.cs

The build method adds by itself the IServiceProvider type with the native instance. As there seem to have no way to modify it properly (without reflection on "_table" field I mean :-). It seems to me that the approach is clearly not good.

I think I'll try other dependency injection engines such as Autofac, but I really wanted to keep the native engine that seemed to me really light weight.

Charles HETIER
  • 1,934
  • 16
  • 28