从配置文件

时间:2018-05-10 10:30:59

标签: c# asp.net-core settings configuration-files aspnetboilerplate

我尝试在ASP.NET Boilerplate中设置MailKit来发送电子邮件,但我一直收到此异常,尽管我已在 app.config 文件中添加了设置。

发送电子邮件的代码:

_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:设置&#39; Abp.Net.Mail.DefaultFromAddress&#39;   是null或空!在   Abp.Net.Mail.EmailSenderConfiguration.GetNotEmptySettingValue(字符串   姓名)in   d:\ Github上\ aspnetboilerplate的\ src \ ABP \ NET \邮件\ EmailSenderConfiguration.cs:行   44在Abp.Net.Mail.EmailSenderBase.NormalizeMail(MailMessage邮件)   在   d:\ Github上\ aspnetboilerplate的\ src \ ABP \ NET \邮件\ EmailSenderBase.cs:行   96 at Abp.Net.Mail.EmailSenderBase.Send(MailMessage mail,Boolean   正常化)   d:\ Github上\ aspnetboilerplate的\ src \ ABP \ NET \邮件\ EmailSenderBase.cs:行   73在TaskManagmentApp.Tasks.TaskAppService.GetAll(GetAllTask​​sInput   输入)   C:\用户\ Dopravo \源\回购\ doFramework \ SampleProjects \ TaskManagmentApp \ SRC \ TaskManagmentApp.Application \服务\ TaskAppService.cs:线   36点   Castle.Proxies.Invocations.ITaskAppService_GetAll.InvokeMethodOnTarget()   在Castle.DynamicProxy.AbstractInvocation.Proceed()at   Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation   调用,UnitOfWorkOptions选项)   d:\ Github上\ aspnetboilerplate \ SRC \ ABP \域\ UOW \ UnitOfWorkInterceptor.cs:线   68在Castle.DynamicProxy.AbstractInvocation.Proceed()at   Abp.Auditing.AuditingInterceptor.PerformSyncAuditing(IInvocation   调用,AuditInfo auditInfo)中   d:\ Github上\ aspnetboilerplate的\ src \ ABP \审计\ AuditingInterceptor.cs:行   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>

2 个答案:

答案 0 :(得分:1)

来自Setting Management的文档:

  

必须实现 ISettingStore 接口才能使用设置系统。虽然您可以按照自己的方式实现它,但它已在模块零项目中完全实现。如果未实现,则会从应用程序的配置文件(web.config或app.config)中读取设置,但这些设置无法更改。范围界定也不起作用。

要回退配置文件,子类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);
}

答案 1 :(得分:0)

我终于能够通过实现从配置文件中读取的自己的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);
}