gpt4 book ai didi

c# - ASP.NET Core 3.1 将 appsettings.json 中的对象数组注入(inject)到注入(inject)的服务中

转载 作者:行者123 更新时间:2023-12-04 12:24:32 25 4
gpt4 key购买 nike

我正在尝试将 appsettings.json 中的几个电子邮件帐户注入(inject)电子邮件服务。

编辑:我的 EmailRepository.cs需要一个 DbContext也注入(inject)。我使用了 @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;
}
}

启动.cs:

public void ConfigureServices(IServiceCollection services)
{
services.Configure<List<SanSender>>((settings) =>
{
Configuration.GetSection("SanSenders").Bind(settings);
});

services.AddScoped<EmailRepository>();

services.AddControllersWithViews();
}

Controller :

public class HomeController : Controller
{
public HomeController(
IOptions<List<SanSender>> optionsSanSenders, EmailRepository emailService)
{
}

public IActionResult Index()
{
return View();
}
}

在 Controller 中我添加了 IOptions<List<SanSender>> optionsSanSenders我可以在那里访问它们,但是 EmailService.cs是在一个类库中,我宁愿不向它添加不必要的依赖项。

EmailService 确实有 IEnumerable<SanSender> SanSenders ,但它的长度/计数为零。

我究竟做错了什么?

最佳答案

改变方法。

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();
}

存储库将解析注入(inject)的发件人。

应该重构 Controller 以获取配置的选项。
public class HomeController : Controller {
public HomeController(IOptions<SanSenderOptions>> optionsSanSenders,
EmailRepository emailService) {
var senders = options.Value.SanSenders; //<--
}

public IActionResult Index() {
return View();
}

}

引用 Configuration in ASP.NET Core

引用 Options pattern in ASP.NET Core

关于c# - ASP.NET Core 3.1 将 appsettings.json 中的对象数组注入(inject)到注入(inject)的服务中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60979175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com