[英]ASP.NET Core 3.1 injecting array of objects from appsettings.json into an injected service
I'm trying to inject a couple email accounts that I have in appsettings.json into an email service.我正在尝试将 appsettings.json 中的几个电子邮件帐户注入电子邮件服务。
EDIT: My EmailRepository.cs
needs a DbContext
injected as well.编辑:我的
EmailRepository.cs
需要注入DbContext
。 I used @Nkosi 's answer, which worked without needing a DbContext.我使用了 @Nkosi 的答案,它不需要 DbContext 就可以工作。 I'm planning on using
DbContextPool
in production, so how do I pull one of those out in my ConfigureServices
method?我计划在生产中使用
DbContextPool
,那么如何在我的ConfigureServices
方法中提取其中之一?
appsettings.json: 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: 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: 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:启动.cs:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<List<SanSender>>((settings) =>
{
Configuration.GetSection("SanSenders").Bind(settings);
});
services.AddScoped<EmailRepository>();
services.AddControllersWithViews();
}
The Controller:控制器:
public class HomeController : Controller
{
public HomeController(
IOptions<List<SanSender>> optionsSanSenders, EmailRepository emailService)
{
}
public IActionResult Index()
{
return View();
}
}
In the controller I added IOptions<List<SanSender>> optionsSanSenders
and I can get access to them there, but EmailService.cs
is in a class library and I would prefer to not add unnecessary dependencies to it.在控制器中,我添加了
IOptions<List<SanSender>> optionsSanSenders
并且我可以在那里访问它们,但是EmailService.cs
在一个类库中,我不想向它添加不必要的依赖项。
The EmailService does have IEnumerable<SanSender> SanSenders
, but it has a zero length/count. EmailService 确实有
IEnumerable<SanSender> SanSenders
,但它的长度/计数为零。
What am I doing wrong?我究竟做错了什么?
Change the approach.改变方法。
public class SanSenderOptions {
public List<SanSender> SanSenders { get; set; }
}
Extract the settings from configuration and then register them where needed从配置中提取设置,然后在需要的地方注册它们
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();
}
The repository will get the senders injected being resolved.存储库将解析注入的发件人。
The controller should be refactored to get the configured options.应该重构控制器以获取配置的选项。
public class HomeController : Controller {
public HomeController(IOptions<SanSenderOptions>> optionsSanSenders,
EmailRepository emailService) {
var senders = options.Value.SanSenders; //<--
}
public IActionResult Index() {
return View();
}
}
Reference Configuration in ASP.NET Core ASP.NET Core 中的参考配置
Reference Options pattern in ASP.NET Core ASP.NET Core 中的参考选项模式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.