【发布时间】:2019-10-03 23:20:57
【问题描述】:
当我收到异常时,我想在 Startup.cs 文件中的 Configure 方法中发送一封电子邮件。
我正在使用 IEmailSender 并将其注册到 Startup.cs 文件中,就像应用程序的其余部分一样。
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IEmailSender, EmailSender>();
}
但是现在我想在收到异常时在 Configure 方法中使用它,但我不确定如何实例化它和/或使用 _emailSender,它是一个 IEmailSender,通常被注入到使用它的类,但在这种情况下(Startup.cs)我无法注入它,因为直到 ConfigureServices 方法运行并将其添加为服务之前它尚未定义。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Seed seeder)
{
app.UseExceptionHandler(builder => { builder.Run(async context => {
// where _emailSender is a IEmailSender emailSender
await _emailSender.SendEmailAsync("someone@gmail.com", "some subject", "hey an exception error!");
});
});
}
【问题讨论】:
标签: c# asp.net-core