I have the following working in a .net core 2.1 aspnet application
using System; using System.IO; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; public class Program { public static void Main(string[] args) { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true) .Build();
When I attempt something similar with a .net core 3.1 WInforms application I see that SetBasePath and AddJsonFile are not available
using Microsoft.Extensions.Configuration; using System; using System.Text; using System.Windows.Forms; using System.Xml; namespace myapp { public partial class Form1 : Form { private SyndicationFeed feed; public Form1() { InitializeComponent(); var configuration = new ConfigurationBuilder(); configuration.SetBasePath(System.IO.Directory.GetCurrentDirectory()); // errors here configuration.AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true); // errors here configuration.Build(); }
Used to build key/value-based configuration settings for use in an application.
Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings. json.
I tried the same code that you have posted. I could able to compile and run the app after adding the following NuGet packages.
Microsoft.Extensions.Configuration (Version 3.1.0)
Microsoft.Extensions.Configuration.Json (Version 3.1.0)
Most probably you are missing these packages in your environment.
You extension method SetBasePath is defined in Config.FileExtensions
.
Can you check if you are missing a reference to: Microsoft.Extensions.Configuration.FileExtensions
package.
For resolving AddJsonFile, need to add a reference to the Microsoft.Extensions.Configuration.Json
package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With