You can use new directly
app.ConfigureExceptionHandler(new LoggerManager());
Pass dependency to app.(AnyMethod) in program.cs in .NET 6
In .Net 5 and previous, we used to have a startup.cs file, with ConfigureServices and Configure Method inside. In below function I have added ILoggerManager as parameter of the function and then passed it to app.ConfigureExceptionHandler function.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerManager logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.ConfigureExceptionHandler(logger);
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
But with .Net 6 there is no startup.cs file and only program.cs file. There is no ConfigureService or Configure methods inside program.cs and all methods or functions are being called in a procedural way.
var builder = WebApplication.CreateBuilder(args);
var logger = new LoggerManager();
// Add services to the container.builder.Services.AddControllers();
builder.Services.AddDbContext<DocumentDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DocumentStore")));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<ILoggerManager, LoggerManager>();var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}app.ConfigureExceptionHandler(<how to pass dependency here>);
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
My question is how can I pass dependency to app.ConfigureExceptionHandler function in .Net 6. I could not find any documentation on it.
Developer technologies | ASP.NET | ASP.NET Core
2 answers
Sort by: Most helpful
-
Bhadresh Dudhat 1 Reputation point
2021-11-16T20:04:34.247+00:00 -
Bruce (SqlWork.com) 82,856 Reputation points Volunteer Moderator
2021-11-07T17:02:11.683+00:00 I don’t understand the question. 10 lines up you defined the object you want injected For example logger is defined, you don’t need to look it up in services. As only singletons made sense in Configure, you just need a reference like logger before adding to services.