Rystem is a open-source framework to improve the System namespace in .Net
$ dotnet add package Rystem.DependencyInjection.Web builder.Services.AddRuntimeServiceProvider();
and after the build use the runtime provider
var app = builder.Build();
app.UseRuntimeServiceProvider();
await RuntimeServiceProvider.GetServiceCollection()
.AddSingleton<Service2>()
.ReBuildAsync();