【问题标题】:Get settings from configuration file从配置文件中获取设置
【发布时间】:2018-05-14 17:02:58
【问题描述】:

我正在尝试在 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: 'Abp.Net.Mail.DefaultFromAddress' 的设置值 为空或为空!在 Abp.Net.Mail.EmailSenderConfiguration.GetNotEmptySettingValue(字符串 名称)在 D:\Github\aspnetboilerplate\src\Abp\Net\Mail\EmailSenderConfiguration.cs:line 44 在 Abp.Net.Mail.EmailSenderBase.NormalizeMail(MailMessage 邮件) 在 D:\Github\aspnetboilerplate\src\Abp\Net\Mail\EmailSenderBase.cs:line 96 在 Abp.Net.Mail.EmailSenderBase.Send(MailMessage 邮件,布尔值 标准化)在 D:\Github\aspnetboilerplate\src\Abp\Net\Mail\EmailSenderBase.cs:line 73 在 TaskManagmentApp.Tasks.TaskAppService.GetAll(GetAllTask​​sInput 输入)在 C:\Users\Dopravo\source\repos\doFramework\SampleProjects\TaskManagmentApp\src\TaskManagmentApp.Application\Services\TaskAppService.cs:line 36 在 Castle.Proxies.Invocations.ITaskAppService_GetAll.InvokeMethodOnTarget() 在 Castle.DynamicProxy.AbstractInvocation.Proceed() 在 Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation 调用,UnitOfWorkOptions 选项)在 D:\Github\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 68 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Auditing.AuditingInterceptor.PerformSyncAuditing(IInvocation 调用,AuditInfo auditInfo) 在 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>

【问题讨论】:

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


    【解决方案1】:

    来自Setting Management上的文档:

    ISettingStore接口必须实现才能使用设置系统。虽然您可以用自己的方式实现它,但它已在 Module Zero 项目中完全实现。如果未实施,则从应用程序的配置文件(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);
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 您的意思是子类化SettingStore 对您不起作用?
      • 我是一种方式。只是零模块中的 SettingStore 不适合我。我想说我最终发布的答案对我有用。
      猜你喜欢
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2011-01-14
      • 2012-07-22
      相关资源
      最近更新 更多