从配置文件中获取设置

Get settings from configuration file

我正在尝试在 ASP.NET 样板中设置 MailKit 以发送电子邮件,但尽管我在 app.config[=23 中添加了设置,但我仍然收到此异常=] 文件.

发送电子邮件的代码:

_emailSender.Send(
    to: "*****@gmail.com",
    subject: "You have a new task!",
    body: $"A new task is assigned for you: <b>Create doFramework</b>",
    isBodyHtml: true
);

收到异常:

{Abp.AbpException: Setting value for 'Abp.Net.Mail.DefaultFromAddress' is null or empty! at Abp.Net.Mail.EmailSenderConfiguration.GetNotEmptySettingValue(String name) in D:\Github\aspnetboilerplate\src\Abp\Net\Mail\EmailSenderConfiguration.cs:line 44 at Abp.Net.Mail.EmailSenderBase.NormalizeMail(MailMessage mail) in D:\Github\aspnetboilerplate\src\Abp\Net\Mail\EmailSenderBase.cs:line 96 at Abp.Net.Mail.EmailSenderBase.Send(MailMessage mail, Boolean normalize) in D:\Github\aspnetboilerplate\src\Abp\Net\Mail\EmailSenderBase.cs:line 73 at TaskManagmentApp.Tasks.TaskAppService.GetAll(GetAllTasksInput input) in C:\Users\Dopravo\source\repos\doFramework\SampleProjects\TaskManagmentApp\src\TaskManagmentApp.Application\Services\TaskAppService.cs:line 36 at Castle.Proxies.Invocations.ITaskAppService_GetAll.InvokeMethodOnTarget() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\Github\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 68 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Auditing.AuditingInterceptor.PerformSyncAuditing(IInvocation invocation, AuditInfo auditInfo) in D:\Github\aspnetboilerplate\src\Abp\Auditing\AuditingInterceptor.cs:line 51}

app.config 文件:

<configuration>
  <runtime>
    <gcServer enabled="true"/>
  </runtime>
  <appSettings>
    <add key="Abp.Net.Mail.DefaultFromAddress" value="lkaddoura@dopravo.com"/>
    <add key="Abp.Net.Mail.DefaultFromDisplayName" value="Lutfi Kaddoura"/>
  </appSettings>     
</configuration>

来自 Setting Management 上的文档:

The ISettingStore interface must be implemented in order to use the setting system. While you can implement it in your own way, it's fully implemented in the Module Zero project. If it's not implemented, settings are read from the application's configuration file (web.config or app.config) but those settings cannot be changed. Scoping will also not work.

要回退配置文件,继承 SettingStore 并覆盖 GetSettingOrNullAsync:

public class MySettingStore : SettingStore
{
    public MySettingStore(
        IRepository<Setting, long> settingRepository,
        IUnitOfWorkManager unitOfWorkManager)
        : base(settingRepository, unitOfWorkManager)
    {
    }

    public override Task<SettingInfo> GetSettingOrNullAsync(int? tenantId, long? userId, string name)
    {
        return base.GetSettingOrNullAsync(tenantId, userId, name)
            ?? DefaultConfigSettingStore.Instance.GetSettingOrNullAsync(tenantId, userId, name);
    }
}

然后替换模块中的 ISettingStore

// using Abp.Configuration.Startup;

public override void PreInitialize()
{
    Configuration.ReplaceService<ISettingStore, MySettingStore>(DependencyLifeStyle.Transient);
}

我终于能够通过实现我自己的从配置文件读取的 SettingStore 来读取设置。请注意,需要实施 GetAllListAsync 调用。

   public class MySettingStore : ISettingStore
    {
        public Task CreateAsync(SettingInfo setting)
        {
            throw new NotImplementedException();
        }

        public Task DeleteAsync(SettingInfo setting)
        {
            throw new NotImplementedException();
        }

        public Task<List<SettingInfo>> GetAllListAsync(int? tenantId, long? userId)
        {
            var result = new List<SettingInfo>();
            var keys = ConfigurationManager.AppSettings.AllKeys;

            foreach (var key in keys)
            {
                result.Add(new SettingInfo(null, null, key, ConfigurationManager.AppSettings[key]));
            }

            return Task.FromResult(result);
        }

        public Task<SettingInfo> GetSettingOrNullAsync(int? tenantId, long? userId, string name)
        {
            var value = ConfigurationManager.AppSettings[name];

            if (value == null)
            {
                return Task.FromResult<SettingInfo>(null);
            }

            return Task.FromResult(new SettingInfo(tenantId, userId, name, value));
        }

        public Task UpdateAsync(SettingInfo setting)
        {
            throw new NotImplementedException();
        }
    }

需要在模块的 PreInitalize() 中替换 MySettingStore。

public override void PreInitialize()
{
    Configuration.ReplaceService<ISettingStore, MySettingStore>(DependencyLifeStyle.Transient);
}