ASP.NET Core 3.1 将 appsettings.json 中的对象数组注入注入服务
ASP.NET Core 3.1 injecting array of objects from appsettings.json into an injected service
我正在尝试将我在 appsettings.json 中的几个电子邮件帐户注入电子邮件服务。
编辑: 我的 EmailRepository.cs
也需要 DbContext
注入。我使用了@Nkosi 的答案,它不需要 DbContext 就可以工作。我计划在生产中使用 DbContextPool
,那么如何在我的 ConfigureServices
方法中提取其中一个?
appsettings.json:
"SanSenders": [
{
"Host": "mail.theFourSeasons.com",
"FromName": "The Four Seasons",
"IsNoReply": true,
"IsSssl": true,
"Password": "tooGoodToBeTrue",
"Port": 465,
"Username": "noreply@theFourSeasons.com"
},
{
"Host": "mail.theFourSeasons.com",
"FromName": "Franki",
"IsNoReply": false,
"IsSssl": true,
"Password": "cantTakeMyEyesOffYou",
"Port": 465,
"Username": "franki@theFourSeasons.com"
}
]
SanSender.cs:
public class SanSender
{
public string FromName { get; set; }
public string Host { get; set; }
public bool IsNoReply { get; set; }
public bool IsSsl { get; set; }
public string Password { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public async Task<bool> SendEmailAsync(
string toAddress, string subject, string htmlMessage)
{
throw new NotImplementedException();
}
}
EmailRepository.cs:
public class EmailRepository
{
public IEnumerable<SanSender> SanSenders { get; set; }
//Edit: I need a DbContext injected as well.
public EmailRepository(ApplicationDbContext applicationDbContext,
IEnumerable<SanSender> sanSenders)
{
SanSenders = sanSenders;
}
}
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<List<SanSender>>((settings) =>
{
Configuration.GetSection("SanSenders").Bind(settings);
});
services.AddScoped<EmailRepository>();
services.AddControllersWithViews();
}
控制者:
public class HomeController : Controller
{
public HomeController(
IOptions<List<SanSender>> optionsSanSenders, EmailRepository emailService)
{
}
public IActionResult Index()
{
return View();
}
}
在控制器中,我添加了 IOptions<List<SanSender>> optionsSanSenders
,我可以在那里访问它们,但是 EmailService.cs
在 class 库中,我不想向它添加不必要的依赖项.
EmailService 确实有 IEnumerable<SanSender> SanSenders
,但它有一个零 length/count。
我做错了什么?
改变方法。
public class SanSenderOptions {
public List<SanSender> SanSenders { get; set; }
}
从配置中提取设置,然后在需要的地方注册它们
public void ConfigureServices(IServiceCollection services) {
SanSender[] senders = Configuration.GetSection("SanSenders").Get<SanSender[]>();
services.Configure<SanSenderOptions>(options => {
options.SanSenders = senders.ToList();
});
services.AddScoped<EmailRepository>(sp =>
new EmailRepository(sp.GetRequiredService<ApplicationDbContext>(), senders));
services.AddControllersWithViews();
}
存储库将解析注入的发件人。
应重构控制器以获取配置的选项。
public class HomeController : Controller {
public HomeController(IOptions<SanSenderOptions>> optionsSanSenders,
EmailRepository emailService) {
var senders = options.Value.SanSenders; //<--
}
public IActionResult Index() {
return View();
}
}
我正在尝试将我在 appsettings.json 中的几个电子邮件帐户注入电子邮件服务。
编辑: 我的 EmailRepository.cs
也需要 DbContext
注入。我使用了@Nkosi 的答案,它不需要 DbContext 就可以工作。我计划在生产中使用 DbContextPool
,那么如何在我的 ConfigureServices
方法中提取其中一个?
appsettings.json:
"SanSenders": [
{
"Host": "mail.theFourSeasons.com",
"FromName": "The Four Seasons",
"IsNoReply": true,
"IsSssl": true,
"Password": "tooGoodToBeTrue",
"Port": 465,
"Username": "noreply@theFourSeasons.com"
},
{
"Host": "mail.theFourSeasons.com",
"FromName": "Franki",
"IsNoReply": false,
"IsSssl": true,
"Password": "cantTakeMyEyesOffYou",
"Port": 465,
"Username": "franki@theFourSeasons.com"
}
]
SanSender.cs:
public class SanSender
{
public string FromName { get; set; }
public string Host { get; set; }
public bool IsNoReply { get; set; }
public bool IsSsl { get; set; }
public string Password { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public async Task<bool> SendEmailAsync(
string toAddress, string subject, string htmlMessage)
{
throw new NotImplementedException();
}
}
EmailRepository.cs:
public class EmailRepository
{
public IEnumerable<SanSender> SanSenders { get; set; }
//Edit: I need a DbContext injected as well.
public EmailRepository(ApplicationDbContext applicationDbContext,
IEnumerable<SanSender> sanSenders)
{
SanSenders = sanSenders;
}
}
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<List<SanSender>>((settings) =>
{
Configuration.GetSection("SanSenders").Bind(settings);
});
services.AddScoped<EmailRepository>();
services.AddControllersWithViews();
}
控制者:
public class HomeController : Controller
{
public HomeController(
IOptions<List<SanSender>> optionsSanSenders, EmailRepository emailService)
{
}
public IActionResult Index()
{
return View();
}
}
在控制器中,我添加了 IOptions<List<SanSender>> optionsSanSenders
,我可以在那里访问它们,但是 EmailService.cs
在 class 库中,我不想向它添加不必要的依赖项.
EmailService 确实有 IEnumerable<SanSender> SanSenders
,但它有一个零 length/count。
我做错了什么?
改变方法。
public class SanSenderOptions {
public List<SanSender> SanSenders { get; set; }
}
从配置中提取设置,然后在需要的地方注册它们
public void ConfigureServices(IServiceCollection services) {
SanSender[] senders = Configuration.GetSection("SanSenders").Get<SanSender[]>();
services.Configure<SanSenderOptions>(options => {
options.SanSenders = senders.ToList();
});
services.AddScoped<EmailRepository>(sp =>
new EmailRepository(sp.GetRequiredService<ApplicationDbContext>(), senders));
services.AddControllersWithViews();
}
存储库将解析注入的发件人。
应重构控制器以获取配置的选项。
public class HomeController : Controller {
public HomeController(IOptions<SanSenderOptions>> optionsSanSenders,
EmailRepository emailService) {
var senders = options.Value.SanSenders; //<--
}
public IActionResult Index() {
return View();
}
}