ConfigurationBuilder for Core Api from legacy dotnet framework 4.7.1 app

987 views Asked by At

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.

2

There are 2 answers

0
8128 On

I just encountered this 'issue'. My problem was that I had:

using System.Configuration;

but what I need is:

using Microsoft.Extensions.Configuration;
2
Nkosi On

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);