在非控制器类中使用依赖注入? [英] Using dependency injection in a non-controller class?

查看:203
本文介绍了在非控制器类中使用依赖注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DocumentRenderer类,该类调用外部API。 DocumentRenderer需要一个AccessKey,该密钥存储在我的appsettings.json配置文件中。我想要一个新实例化的DocumentRenderer对象,默认情况下使用配置文件中指定的AccessKey。但是,我不知道如何在startup.cs之外实现此目标。 (我正在使用ASP.NET Core)

I have a DocumentRenderer class, which calls an external API. The DocumentRenderer requires an AccessKey, which is stored in my appsettings.json config file. I want a newly instantiated DocumentRenderer object to, by default, use the AccessKey specified in the config file. However, I can't figure out how to achieve this outside of startup.cs. (I'm using ASP.NET Core)

这是到目前为止我尝试过的内容:

Here's what I've tried so far:

已添加到appsettings.json的DocumentRenderer:

Added DocumentRenderer to appsettings.json:

"DocumentRenderer": {
    "AccessKey": "<key>",
    "EndpointUrl": "<url>",
    "OutputFormat" :  "pdf" 
}

创建了一个 DocumentRendererOptions POCO:

Created a "DocumentRendererOptions" POCO:

public class DocumentRendererOptions
{
    public string AccessKey { get; set; }
    public string EndpointUrl { get; set; }
    public string OutputFormat { get; set; }
}

将DocumentRendererOptions注册为单例服务,并将appsettings.json中的选项绑定到它在startup.cs的ConfigureServices方法中:

Registered DocumentRendererOptions as a singleton service and bound the options from appsettings.json to it in the ConfigureServices method of startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<DocumentRendererOptions>();
    services.Configure<DocumentRendererOptions>(options => Configuration.GetSection("DocumentRenderer").Bind(options));
}

最后,我有了我的DocumentRenderer类:

Finally I have my DocumentRenderer class:

public class DocumentRenderer
{
    private readonly string _endpointUrl;
    private readonly string _accessKey;
    private readonly string _outputFormat;

    public DocumentRenderer()
    {
    }

    public DocumentRenderer(IOptions<DocumentRendererOptions> options)
    {
        _accessKey = options.Value.AccessKey;
        _endpointUrl = options.Value.EndpointUrl;
        _outputFormat = options.Value.OutputFormat;
    }
}

我错误地认为这将允许我实例化一个新的具有默认选项的DocumentRenderer对象,但是显然缺少某些东西。

I incorrectly assumed this would allow me to instantiate a new DocumentRenderer object with the default options, but obviously something is missing.

到目前为止,我读过的每篇文章都只是在谈论使用此方法和控制器并允许DI做

Every article I've read so far just talks about using this method with a controller and allowing DI to do the rest, but the DocumentRenderer isn't a controller.

作为一个临时修复,我刚刚将DocumentRendererOptions设为静态,然后在启动时分配了值,但这并没有。似乎是最好的解决方案

As a temporary fix I've just made DocumentRendererOptions static, then assigned the values in startup, but this doesn't seem like the best solution

推荐答案

DocumentRenderer 注册为以下服务好,这样框架就会为您实例化

register the DocumentRenderer with services as well so the framework will instantiate it for you

public void ConfigureServices(IServiceCollection services)
{
    // Adds services required for using options.
    services.AddOptions();

    // Registers the following lambda used to configure options.
    services.Configure<DocumentRendererOptions>(Configuration.GetSection("DocumentRenderer"));

    //register other services
    services.AddSingleton<DocumentRenderer>();
}

来源:使用选项和配置对象

这篇关于在非控制器类中使用依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆