Reputation: 2009
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
Upvotes: 23
Views: 23537
Reputation: 832
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
Upvotes: 36