Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Use ConfigurationBuilder in .net core 3.1 Winforms?

Tags:

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();     } 
like image 889
Kirsten Avatar asked Dec 20 '19 04:12

Kirsten


People also ask

What is Configurationbuilder in .NET core?

Used to build key/value-based configuration settings for use in an application.

Can you use app config in .net core?

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.


2 Answers

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.

like image 113
Thangadurai Avatar answered Sep 19 '22 13:09

Thangadurai


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.

like image 35
Gauravsa Avatar answered Sep 17 '22 13:09

Gauravsa