如何在Autoac模块中注册依赖于外部文件中配置值的服务

时间:2019-05-12 08:55:07

标签: c# .net .net-core autofac

我正在使用Autofac和点网核心构建控制台应用程序。

我要注册的服务之一是我构建的API服务。它的构造函数采用一些参数,这些参数是存储在配置文件中的值,我可以通过 100 / \ 20 500 / \ 10 30 (在程序启动时注册)进行访问。

如何注册服务,使构造函数具有ConfigurationBuilder中可用的参数?

例如,我的服务的构造函数是:

IConfigurationRoot

我正在如下public DearInventoryApiService(ILogger<DearInventoryApiService> logger, string baseUrl, string accountId, string applicationKey) 中注册容器:

Program.cs

我的 private static void RegisterServices() { var collection = new ServiceCollection(); collection.AddLogging(configure => configure.AddConsole()); //register the autofac container provider var containerBuilder = new ContainerBuilder(); //register the default module containerBuilder.RegisterModule(new DefaultDependencyModule()); containerBuilder.Populate(collection); var container = containerBuilder.Build(); ServiceProvider = new AutofacServiceProvider(container); } 如下所示:

DefaultDependencyModule

我的上述问题是我不确定如何将配置值获取到模块中进行注册?

编辑1:

我已经更改了模块以传递配置。它可以工作,但是我不确定这是不好的设计还是有更好的东西?

public class DefaultDependencyModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.Register(r => new DearInventoryApiService(r.Resolve<ILogger<DearInventoryApiService>>(), {base url here}, {account id here}, {application key here})).As<IDearInventoryService>();

    }
}

1 个答案:

答案 0 :(得分:0)

选项1

您的编辑看起来不错,但是根据Single Responsibility Principle,我认为最好在ApplicationConfiguration访问和DefaultDependencyModule之间脱钩。

public class DefaultDependencyModule : Module
{
    public string BaseUrl { get; set; }
    public string AccountId { get; set; }
    public string ApplicationKey { get; set; }

    protected override void Load(ContainerBuilder builder)
    {
        //dear inventory api service
        builder.Register(r =>
        {
            var logger = r.Resolve<ILogger<DearInventoryApiService>>();

            return new DearInventoryApiService(logger, BaseUrl, AccountId, ApplicationKey);
        }).As<IDearInventoryService>();   
    }
}

并使用以下命令修改RegisterServices

private static void RegisterServices()
{
    string baseUrl = ApplicationConfiguration["dearInventory:apiBaseUrl"];
    string accountId = ApplicationConfiguration["dearInventory:apiAccountId"];
    string applicationKey = ApplicationConfiguration["dearInventory:apiApplicationKey"];

    //register the default module
    containerBuilder.RegisterModule(new DefaultDependencyModule() {
        BaseUrl = baseUrl,
        AccountId = accountId,
        ApplicationKey = applicationKey
    });

    //code
});

选项2:

public class DefaultDependencyModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        //dear inventory api service
        builder.Register((c, p) =>
        {
            string baseUrl = p.Named<string>("baseUrl");
            string accountId = p.Named<string>("accountId");
            string baseUrl = p.Named<string>("baseUrl");
            var logger = r.Resolve<ILogger<DearInventoryApiService>>();

            return new DearInventoryApiService(logger, baseUrl, accountId, applicationKey);
        })
        .As<IDearInventoryService>();   
    }
}

解决时间:

string baseUrl = ApplicationConfiguration["dearInventory:apiBaseUrl"];
string accountId = ApplicationConfiguration["dearInventory:apiAccountId"];
string applicationKey = ApplicationConfiguration["dearInventory:apiApplicationKey"];

DearInventoryService service = c.Resolve<DearInventoryService>(
    new NamedParameter("baseUrl", baseUrl), 
    new NamedParameter("accountId", accountId), 
    new NamedParameter("applicationKey", applicationKey)
);