Reputation: 699
Trying to create a IConfigurationRoot
type object to consume Core 2.2 Api from legacy dotnet framework 4.7.1 WPF app config file.
myCoreApiController.CoreApiMethod( confirutation); // (IConfigurationRoot configuration)
First defined a Dictionary and load it with keys that identify your configuration settings and values (the actual settings). That code looks like this:
var settings = new Dictionary<string, string>
{
{"toDoService:url", "http://..."},
{"toDoService:contractid", "???"}
};
Then created a ConfigurationBuilder
, add my Dictionary of configuration settings to it and use that ConfigurationBuilder
to build an IConfiguration
object.
Here's that code:
var cfgBuilder = new ConfigurationBuilder();
cfgBuilder.AddInMemoryCollection(settings);
IConfiguration cfg = cfgBuilder.Build();
But the ConfigurationBuilder
is also an Interface. So I can't create instance of that.
Then I tried to implement that and create my own but could not over override AddInMemoryCollection
static method.
So the upper solution is not working.
Please note that the project has the NuGet Microsoft.Extensions.Configuration, Microsoft.Extensions.Configuration.Abstractions are installed.
Hope, some one will share their way to achieve that.
Upvotes: 1
Views: 1209
Reputation: 999
I just encountered this 'issue'. My problem was that I had:
using System.Configuration;
but what I need is:
using Microsoft.Extensions.Configuration;
Upvotes: 3
Reputation: 247561
ConfigurationBuilder.Build
method returns IConfigurationRoot
, which is derived from IConfiguration
but in your code, you assign the built configuration to IConfiguration
IConfiguration cfg = cfgBuilder.Build();
it was indicated that the target method is defined as
CoreApiMethod(IConfigurationRoot configuration);
The issue is trying to pass the assigned IConfiguration
variable to the target function as IConfigurationRoot
.
Use the appropriate type when assigning the variable.
var settings = new Dictionary<string, string> {
{"toDoService:url", "http://..."},
{"toDoService:contractid", "???"}
};
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddInMemoryCollection(settings)
.Build();
myCoreApiController.CoreApiMethod(confirutation);
Upvotes: 0