How can I create an instance of IConfiguration loc

2019-08-21 02:49发布

I'd want to ask how to create an instance of ASP.NET Core's Configuration, the same that's being created when I require it in Controller's constructor which knows about the appsettings.json file

like _config = ActivatorUtilities.CreateInstance<IConfiguration>(null);

System.InvalidOperationException: 'A suitable constructor for type 'Microsoft.Extensions.Configuration.IConfiguration' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.'

It makes sense because it is interface, but how it does work in Controller's Constructor case? and how can I create an instance of that?

I'm using MS DI

Thanks

1条回答
家丑人穷心不美
2楼-- · 2019-08-21 03:07

You can create a local instance of configuration as shown below.

IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath([PATH_WHERE_appsettings.json_RESIDES])
            .AddJsonFile("appsettings.json")
            .Build();

For further information see Configuration in ASP.NET Core

查看更多
登录 后发表回答