【发布时间】:2021-11-05 17:27:40
【问题描述】:
在 .Net 5 及之前的版本中,我们曾经有一个 startup.cs 文件,其中包含 ConfigureServices 和 Configure Method。在下面的函数中,我添加了 ILoggerManager 作为函数的参数,然后将其传递给 app.ConfigureExceptionHandler 函数。
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();
});
}
但是对于 .Net 6,没有 startup.cs 文件,只有 program.cs 文件。 program.cs 中没有 ConfigureService 或 Configure 方法,所有方法或函数都以程序方式调用,没有任何类或方法声明,如下所示:
var builder = WebApplication.CreateBuilder(args);
var logger = new LoggerManager();
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();
我的问题是如何将依赖项传递给 .Net 6 中的 app.ConfigureExceptionHandler() 函数。我找不到任何文档。
【问题讨论】:
标签: c# asp.net asp.net-core asp.net-core-webapi .net-6.0