C# ASP.NET Core 3.1将appsettings.json中的对象数组注入注入服务
我正在尝试将appsettings.json中的几个电子邮件帐户注入电子邮件服务 EDIT:MyC# ASP.NET Core 3.1将appsettings.json中的对象数组注入注入服务,c#,dependency-injection,asp.net-core-3.1,C#,Dependency Injection,Asp.net Core 3.1,我正在尝试将appsettings.json中的几个电子邮件帐户注入电子邮件服务 EDIT:MyEmailRepository.cs也需要注入DbContext。我使用了@Nkosi的答案,它在不需要DbContext的情况下工作。我计划在生产中使用DbContextPool,那么如何在ConfigureServices方法中提取其中一个呢 appsettings.json: "SanSenders": [ { "Host": "mail.theFourSeasons.com",
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:
公共类SanSender
{
公共字符串FromName{get;set;}
公共字符串主机{get;set;}
公共bool IsNoReply{get;set;}
公共布尔IsSsl{get;set;}
公共字符串密码{get;set;}
公共int端口{get;set;}
公共字符串用户名{get;set;}
公共异步任务SendEmailAsync(
字符串地址、字符串主题、字符串htmlMessage)
{
抛出新的NotImplementedException();
}
}
EmailRepository.cs:
公共类电子邮件存储库
{
公共IEnumerable sansender{get;set;}
//编辑:我还需要一个DbContext注入。
公共电子邮件存储库(ApplicationDbContext ApplicationDbContext,
IEnumerable sanSenders)
{
SanSenders=SanSenders;
}
}
Startup.cs:
public void配置服务(IServiceCollection服务)
{
服务。配置((设置)=>
{
Configuration.GetSection(“SanSenders”).Bind(设置);
});
services.addScope();
services.AddControllersWithViews();
}
控制员:
公共类HomeController:控制器
{
公共家庭控制器(
IOOptions选项SanSenders,EmailRepository(电子邮件服务)
{
}
公共IActionResult索引()
{
返回视图();
}
}
在控制器中,我添加了IOptions选项sansenders
,我可以在那里访问它们,但是EmailService.cs
在类库中,我不希望向它添加不必要的依赖项
EmailService确实有IEnumerable sansender
,但它的长度/计数为零
我做错了什么?改变方法
public class SanSenderOptions {
public List<SanSender> SanSenders { get; set; }
}
公共类SANDENDEROPTIONS{
公共列表sansender{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 void配置服务(IServiceCollection服务){
SanSender[]senders=Configuration.GetSection(“SanSenders”).Get();
配置(选项=>{
options.SanSenders=senders.ToList();
});
services.AddScoped(sp=>
新的EmailRepository(sp.GetRequiredService
参考Ugh,简化了我的代码以便于阅读。问题是,我的EmailRepository也需要注入DbContext。我将在生产中使用DbContextPool
,所以不想在configure services中新建实例。要更新我的问题。好的,我已经更新了我的问题,以包括需要DbContext@MichaelTranchida 更新的答案。我还假设您注册了DbContext,因为您提供的简化示例中没有显示它。谢谢,我正在四处寻找,试图找到它。这种方法会考虑json文件的重载更改吗?
public class HomeController : Controller {
public HomeController(IOptions<SanSenderOptions>> optionsSanSenders,
EmailRepository emailService) {
var senders = options.Value.SanSenders; //<--
}
public IActionResult Index() {
return View();
}
}