【问题标题】:injecting settings resolved from IOption into additional class in .NET Core将从 IOption 解析的设置注入 .NET Core 中的其他类
【发布时间】:2020-11-24 03:03:23
【问题描述】:

如何注入从 IOptions 值创建的设置来调用某些类方法?

我有MailAppSettings 类和MailSender: IMailSender。两者都在不同的 .NET 项目中。

MailAppSettings 中的值是使用 .NET Core 中的 IOptions 模式加载的。值从 appsettings.json 配置加载。对于 DI,我们使用 AutoFac,所以它看起来像这样:

 serviceCollection.AddOptions();
 serviceCollection.Configure<MailAppSettings>(config.GetSection("MailAppSettings"));

然后我们像这样访问mailSettings

 public class CustomerSender:ICustomerSender
   {
        private readonly MailSettings _mailSettings;
        private readonly IMailSender _mailSender;

        public MyCustomerSender(IOptions<MailSettings> mailSettings, IMailSender mailSender)
        {
            _mailSettings = mailSettings.Value;
            _mailSender = mailSender;
        }

        public SomeCustomMethod1() {
        // use of mailsettings
            var recipient = mailSettings.Recipient1;
            var body = BuildSomeBody();
            var subject = mailSettings.Subject1;

            // mail sending
            _mailSender.Send(recipient, body, subject);
         }

mailSender 是位于不同命名空间中的类,在不同项目的不同位置被调用。

    public class MailSender: IMailSender
    {
        private readonly IMailSenderSettings _mailSenderSettings;

        public MailSender(IMailSenderSettings confg) ///!!! here we need to inject those settings
        {
            _mailSenderSettings= confg;
        }
        ...

在这里,您看到mailSender 需要注入IMailSenderSettingsMailAppSettings 实现了这个接口(但还包含其他属性)。

我的问题是-如何将MailAppSettings 注入IMailSenderSettings? 如果我只是这样注册类型:

containerBuilder.RegisterType<MailAppSettings>()
               .As<IMailSenderSettings>()
               .SingleInstance();

它不起作用,注入的 IMailSenderSettings 的值为 null。

IMailSenderSettings 以及 MailAppSettings 包含 recipientsmtp host, passworduser,但 MailAppSettings 还包含 IMailSenderSettings 不需要的其他属性

【问题讨论】:

  • “因为 MailAppSettings 值仅在 CustomerSender 的构造函数中设置。” - >不清楚你的意思是什么。另外,MailSettings 有什么并不重要,只要它实现了IMailSenderSettings
  • 您需要创建依赖于MailSettingsIMailSenderSettings 派生类,以便可以在组合根中映射所需的成员。从那里只需注册实现,以便 DI 容器可以处理其余部分。
  • @Nkosi 你能给我举个例子吗,我想我不明白你......
  • @CamiloTerevinto 我的意思是,如果我像以前一样注册 DI,mailSender 中的 _mailSenderSettings 值都是空的。

标签: c# .net-core dependency-injection


【解决方案1】:

您需要创建依赖于MailSettingsIMailSenderSettings 派生类

public class MyMailSenderSettings: IMailSenderSettings {
    private readonly MailSettings mailSettings;

    public MyMailSenderSettings(IOptions<MailSettings> options) {
        this.mailSettings = options.Value;
    }

    public string SomeProperty => mailSettings.SomeMatchingProperty;

    //... other members mapped from settings 
}

以便可以在组合根中映射所需的成员。

从那里只需注册实现,以便 DI 容器可以处理其余部分。

//...

serviceCollection.AddOptions();
serviceCollection.Configure<MailAppSettings>(config.GetSection("MailAppSettings"));

serviceCollection.AddSingleton<IMailSenderSettings, MyMailSenderSettings>()

//...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2018-12-21
    • 2022-12-20
    • 2020-09-02
    • 2019-04-13
    相关资源
    最近更新 更多