After registering instances on my IServiceCollection, I need to register an IAutomapperProvider that depends on an IAssemblyProvider that was registered before this method call
public static IServiceCollection RegisterAutomapperConfiguration(this IServiceCollection container, ServiceLifetime lifeTime = ServiceLifetime.Scoped)
{
//creating the provider to get the IAssemblyProvider for my IAutomapperProvider
var prov = container.BuildServiceProvider();
var assemblyProvider = prov.GetService<IAssemblyProvider>();
container.Register<IAutomapperProvider>(aProv => new AutomapperProvider(assemblyProvider), lifeTime);
var autoMapperProvider = prov.GetService<IAutomapperProvider>();
var mapperConfig = autoMapperProvider.GetMapperConfiguration();
...
}
If right after the call of container.Register<IAutomapperProvider>(aProv => new AutomapperProvider(assemblyProvider), lifeTime);
I don't call BuildServiceProvider again, then I would not get the IAutomapperProvider I registered before
public static IServiceCollection RegisterAutomapperConfiguration(this IServiceCollection container, ServiceLifetime lifeTime = ServiceLifetime.Scoped)
{
//creating the provider to get the IAssemblyProvider for my IAutomapperProvider
var prov = container.BuildServiceProvider();
var assemblyProvider = prov.GetService<IAssemblyProvider>();
container.Register<IAutomapperProvider>(aProv => new AutomapperProvider(assemblyProvider), lifeTime);
prov = container.BuildServiceProvider();
var autoMapperProvider = prov.GetService<IAutomapperProvider>();
var mapperConfig = autoMapperProvider.GetMapperConfiguration();
...
}
On the AspNetCore code when you call BuildServiceProvider extension method they use the same IServiceCollection that can change over the time adding more elements, at the end you are pointing to the same reference
public static ServiceProvider BuildServiceProvider(this IServiceCollection services)
{
return BuildServiceProvider(services, ServiceProviderOptions.Default);
}
Then why I need to call it again to get a new instance that knows how to resolve my Service?
To avoid confusions, the Register method is an extension I created but internally calls the AddSinglenton or Add...
public static IServiceCollection Register<TService>(this IServiceCollection container, Func<IServiceProvider, TService> implementationFactory, ServiceLifetime lifeTime)
where TService : class
{
if (container == null)
throw new ArgumentNullException(nameof(container));
if (implementationFactory == null)
throw new ArgumentNullException(nameof(implementationFactory));
switch (lifeTime)
{
case ServiceLifetime.Scoped:
container.AddScoped(typeof(TService), (Func<IServiceProvider, object>)implementationFactory);
break;
case ServiceLifetime.Transient:
container.AddTransient(typeof(TService), (Func<IServiceProvider, object>)implementationFactory);
break;
default:// ServiceLifetime.Singleton
container.AddSingleton(typeof(TService), (Func<IServiceProvider, object>)implementationFactory);
break;
}
return container;
}