I realize DI (ctor based) in .NET CORE is quite straight forward but if it comes to nested injections I struggle.
This is quite simple:
public void ConfigureServices(IServiceCollection services)
{
services.AddRavenDbDocStore(x => x.AfterInitializeDocStore = RavenConfiguration.AfterInitializeDocStore());
services.AddRavenDbAsyncSession();
services.AddSingleton(new ExceptionHelper());
services.AddScoped<ICompoundService>(x => new CompoundService(x.GetService<IAsyncDocumentSession>(), x.GetService<ExceptionHelper>()));
But whenever I need to register a type within e.g a lamba of another registration, I have problems resolving dependencies:
services.AddMvc(setup =>
{
ILogger logger; // how would I get that?
setup.Filters.Add(new ApiExceptionFilter(logger));
}).AddFluentValidation();
Is there a good way to deal with that?
I certainly do not want to call this in ConfigureServices
var sp = services.BuildServiceProvider();
I read about this in Resolving instances with ASP.NET Core DI from within ConfigureServices but I do not reall see an elegant option here...
Update: After reading comments I realized that you can register Filters per type:
setup.Filters.Add<ApiExceptionFilter>()
Then you would not have to pass dependencies in the first place. Still I wonder if there is a best practice for similar scenarios where you cannot access ServiceProvider in the lambda.