简体   繁体   English

将从 IOption 解析的设置注入 .NET Core 中的附加类

[英]injecting settings resolved from IOption into additional class in .NET Core

How to inject Settings created from IOptions value to call some class method?如何注入从 IOptions 值创建的设置以调用某些类方法?

I have MailAppSettings class and MailSender: IMailSender .我有MailAppSettings类和MailSender: IMailSender Both are in different .NET projects.两者都在不同的 .NET 项目中。

Values into MailAppSettings are loaded using IOptions pattern in .NET Core. MailAppSettings中的值使用 .NET Core 中的IOptions模式加载。 Values are loaded from appsettings.json configuration.值从appsettings.json配置加载。 For DI we use AutoFac, so it looks like this:对于 DI,我们使用 AutoFac,所以它看起来像这样:

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

And later then we access mailSettings like this:然后我们像这样访问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 is the class that sits in separate namespace and is called from different places in different projects. mailSender是位于不同命名空间中的类,从不同项目的不同位置调用。

    public class MailSender: IMailSender
    {
        private readonly IMailSenderSettings _mailSenderSettings;

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

Here, you see that mailSender needs IMailSenderSettings to be injected.在这里,您看到mailSender需要注入IMailSenderSettings MailAppSettings implements this interface (but also contains additional properties). MailAppSettings实现此接口(但也包含其他属性)。

My question is- how to inject MailAppSettings into IMailSenderSettings ?我的问题是 - 如何将MailAppSettings注入IMailSenderSettings If I just register type like this:如果我只是注册这样的类型:

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

it won't work, values of injected IMailSenderSettings would be null.它不起作用,注入的 IMailSenderSettings 的值将为空。

IMailSenderSettings , as well as, MailAppSettings contains recipient , smtp host, password , user , but MailAppSettings also contains additional properties not needed for IMailSenderSettings IMailSenderSettings以及MailAppSettings包含recipientsmtp host, passworduser ,但MailAppSettings还包含IMailSenderSettings不需要的其他属性

You will need to create IMailSenderSettings derived class that depends on MailSettings您将需要创建依赖于MailSettings IMailSenderSettings派生类

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 
}

so that the desired members can be mapped in the composition root.以便可以在组合根中映射所需的成员。

From there it is only a matter of registering the implementation so that the DI container can handle the rest.从那里开始,只需要注册实现,以便 DI 容器可以处理其余部分。

//...

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

serviceCollection.AddSingleton<IMailSenderSettings, MyMailSenderSettings>()

//...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM