Search code examples
c#.netwpf.net-core-3.0

Cannot add appsettings.json inside WPF project .net core 3.0


I am creating a WPF project using .net Core 3.0, and I am having trouble adding the item appsettings.json file to my project which is to be used to store my DB connection string.

I would normally have done inside the app.config, but this has now been removed from .net Core.

Everywhere mentions using appsettings.json as a replacement, and that it has to be maunally added & initialised in the OnStartUp() function using an instance of IConfiguration, and there after using Dependency Injection to pass in the config class into the project.

But my issue is that can only add the appsettings.json item on asp.net Core projects? not my WPF solution.

I do apologies if I'm missing something very obvious (which I probably am), I just can't seem to find any solutions.


Solution

  • Steps:

    • To Add the following nuget packages

        Microsoft.Extensions.Configuration
        Microsoft.Extensions.Configuration.FileExtensions
        Microsoft.Extensions.Configuration.Json
        Microsoft.Extensions.DependencyInjection
      
    • You would need to create and add appsettings.json manually and set copy it to output directory as copy if newer


    AppSetting.json

       {
      "ConnectionStrings": {
        "BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
      },
    }
    

    Program.cs (For .NetCore Console App)

    static void Main(string[] args)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
    
        IConfigurationRoot configuration = builder.Build();
    
        Console.WriteLine(configuration.GetConnectionString("BloggingDatabase"));
    }
    

    App.xaml.cs (For .NET CORE WPF)

    public partial class App : Application
    {
        public IServiceProvider ServiceProvider { get; private set; }
     
        public IConfiguration Configuration { get; private set; }
     
        protected override void OnStartup(StartupEventArgs e)
        {
            var builder = new ConfigurationBuilder()
             .SetBasePath(Directory.GetCurrentDirectory())
             .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
     
            Configuration = builder.Build();
    
          Console.WriteLine(Configuration.GetConnectionString("BloggingDatabase"));    
    
            var serviceCollection = new ServiceCollection();
            ConfigureServices(serviceCollection);
     
            ServiceProvider = serviceCollection.BuildServiceProvider();
     
            var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
            mainWindow.Show();
        }
     
        private void ConfigureServices(IServiceCollection services)
        {
            // ...
     
            services.AddTransient(typeof(MainWindow));
        }
    }
    

    References: