0

Thanks for taking the time to look at my question.

I have been sharing a data access layer between my ASP.NET Core 3.1 web app and my .NET Core 3.1 Windows Forms backend app through a class library pattern.

This has been working fine up until I decided to transition the data access layer to be injected using dependency injection.

I have successfully setup the Windows Forms app to use dependency injection, however this version of Windows Forms doesn't use appsettings.json and therefore doesn't use an IConfiguration instance to pass the connection string. Instead I access the connection string through Settings.settings in the solution dependencies like so:

Properties.Settings.Default.MyConnectionString

I have tried to build an IConfiguration object to read from Properties.Settings.Default but I can't seem to find a method to instantiate an IConfigurationSection to populate ConnectionStrings section in IConfiguration so that the

GetConnectionString("MyConnectionString")

call will work.

So my question is: can I reconfigure the Windows Forms .NET Core app to use AppSettings.json? Or is there a way to build an instance of IConfiguration that can be populated by Properties.Settings.Default.MyConnectionString? Or is there a much better way of handling this situation that I haven't considered?

Any help is good help at the moment.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • [mcve] please... – OldProgrammer Apr 12 '22 at 01:20
  • Thanks for the response but I really don't know in this particular instance what can I reproduce? – David Rawek Apr 12 '22 at 01:33
  • I am struggling to see the issue, why would the use of dependency injection change where you can access whatever configuration settings you had before? – Ben Matthews Apr 12 '22 at 03:05
  • Can you find answer in [this](https://stackoverflow.com/questions/114527/simplest-way-to-have-a-configuration-file-in-a-windows-forms-c-sharp-application)? Add key in the configuration/appsettings section, then use System.Configuration.ConfigurationSettings.AppSettings["MyKey"]; – Qing Guo Apr 12 '22 at 03:06
  • @BenMatthews So that I can use the same class library between the two project types. Asp.Net Core uses AppSettings.json kvp accessed through IConfiguration interface whereas the windows forms app uses a different method. I thought about it and I think I am going recreate the windows forms solution as a core 3.1 from the start and import all of my class libraries and forms. It was originally a .net core 5.0 app and I think that might be why I am having problems. If it works I will post that as a solution. – David Rawek Apr 12 '22 at 21:03

1 Answers1

0

In the end, I came across the post, Unable to resolve service for type Microsoft Extensions Configuration IConfiguration, and then just copied the appsettings.json over from my ASP.NET app and everything is working.

All I had to do was add .SetBasePath(@"mypath") and remove some things that didn't apply to my project. Here is the code that did the trick in the end:

var builder = new ConfigurationBuilder()
.SetBasePath(@"mypath").AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            IConfiguration configuration = builder.Build();
services.AddSingleton<IConfiguration>(configuration);`
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77