gpt4 book ai didi

AspNetBoilerplate 不从数据库加载设置

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

我正在使用 aspNetBoilerplate 发送电子邮件(或尝试发送电子邮件)

我已经使用种子数据中的默认设置创建器将设置添加到数据库中,就像这样

namespace ESMPortal.EntityFrameworkCore.Seed.Host
{
public class DefaultSettingsCreator
{
private readonly ESMPortalDbContext _context;

public DefaultSettingsCreator(ESMPortalDbContext context)
{
_context = context;
}

public void Create()
{
// Emailing
AddSettingIfNotExists(EmailSettingNames.DefaultFromAddress, "system@elite-sports-management.co.uk");
AddSettingIfNotExists(EmailSettingNames.DefaultFromDisplayName, "Elite Sports Management");
AddSettingIfNotExists(EmailSettingNames.Smtp.Host, "mail.x.co.uk");
AddSettingIfNotExists(EmailSettingNames.Smtp.Password, "xxx");
AddSettingIfNotExists(EmailSettingNames.Smtp.UserName, "system@x.co.uk");
AddSettingIfNotExists(EmailSettingNames.Smtp.EnableSsl, "true");

// Languages
AddSettingIfNotExists(LocalizationSettingNames.DefaultLanguage, "en");
}

private void AddSettingIfNotExists(string name, string value, int? tenantId = null)
{
if (_context.Settings.IgnoreQueryFilters().Any(s => s.Name == name && s.TenantId == tenantId && s.UserId == null))
{
return;
}

_context.Settings.Add(new Setting(tenantId, null, name, value));
_context.SaveChanges();
}
}
}

我已经确认这些设置是在项目运行时添加到数据库中的

但是,当我尝试发送电子邮件时,我收到一条错误消息,指出位于 127.0.0.1:25 的服务器没有响应。这显然意味着数据库中的设置未被使用。

当我在发送电子邮件时没有包含发件人地址时,这一点得到了进一步证实。我收到一条错误消息,指出 DefaultFromAddress 为 null 或为空。事实并非如此,因为我已确认它存在于数据库设置表中。

这是我用来发送电子邮件的代码。以上错误都是在try语句的catch部分产生的

try
{
_emailSender.Send(
to: userId.EmailAddress,
from: "system@x.co.uk",
subject: "New Upload",
body: $"A new upload is assigned to you: <b>{upload.DisplayName}</b>",
isBodyHtml: true
);
}
catch (Exception e)
{

throw;
}

最佳答案

事实证明我没有正确添加设置。我已将它们添加到种子数据中,尽管它在数据库中可见,但在查询时未返回。通过自定义设置提供程序添加它们解决了这个问题。

namespace ESMPortal.Settings
{
public class CustomSettingProvider : SettingProvider
{
public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
{
return new[]
{
new SettingDefinition(
"Abp.Net.Mail.Smtp.Host",
"mail.x.co.uk"
),


new SettingDefinition(
"Abp.Net.Mail.DefaultFromAddress",
"x@x.co.uk",
scopes: SettingScopes.User,
clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
)
,
new SettingDefinition(
"Abp.Net.Mail.DefaultFromDisplayName",
"XXX",
scopes: SettingScopes.User,
clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
)
,
new SettingDefinition(
"Abp.Net.Mail.Smtp.UserName",
"x@x.co.uk",
scopes: SettingScopes.User,
clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
)
,
new SettingDefinition(
"Abp.Net.Mail.Smtp.Password",
"XXX",
scopes: SettingScopes.User,
clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
)
// ,
// new SettingDefinition(
//"Abp.Net.Mail.Smtp.Domain",
//"x.co.uk",
//scopes: SettingScopes.User,
//clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
//)
// ,
// new SettingDefinition(
//"Abp.Net.Mail.Smtp.EnableSsl",
//"true",
//scopes: SettingScopes.User,
//clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
//)
,
new SettingDefinition(
"Abp.Net.Mail.Smtp.UseDefaultCredentials",
"false",
scopes: SettingScopes.User,
clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
)
};
}
}
}

这在模块的 PreInitialize() 中

    //custom settings provider
Configuration.Settings.Providers.Add<AppSettingProvider>();

关于AspNetBoilerplate 不从数据库加载设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56652889/

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