I've just upgraded a .net core app from version 2.2 to 3. Inside the ConfigureServices method in startup.cs I need to resolve a service that is used by the authentication service. I was "building" all the services using "services.BuildServiceProvider()" but .net core 3 complains about the method creating additional copies of the services and suggesting me to dependency injecting services as parameters to 'configure'. I have no idea what the suggestion means and I'd like to understand it.
public virtual void ConfigureServices(IServiceCollection services)
{
// Need to resolve this.
services.AddSingleton<IManageJwtAuthentication, JwtAuthenticationManager>();
var sp = services.BuildServiceProvider(); // COMPLAINING HERE!!
var jwtAuthManager = sp.GetRequiredService<IManageJwtAuthentication>();
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(c =>
{
c.TokenValidationParameters = new TokenValidationParameters
{
AudienceValidator = jwtAuthManager.AudienceValidator,
// More code here...
};
}
}