0

I inherited a bit of code:

 var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json");

I think is stems from a .net Core application. First I struggled with this bit (in the sense that I could not compile it in .Net Framework):

.SetBasePath(Directory.GetCurrentDirectory())

But I came across this. The accepted answer solved it. Now I am struggling with this bit (again in the sense that I cannot compile it in .Net Framework)::

.AddJsonFile("appsettings.json")

Is there a way to fix this please (usually I would get such data from App.Config ...)? Thanks.

PS:

More code plus error message:

using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration.FileExtensions;
using Microsoft.Extensions.Configuration;

namespace SandboxSecurityToken
{
    class Program
    {
        static void Main(string[] args)
        {

        ...


        static async Task RunAsync()
        {
            var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");       


Severity    Code    Description Project File    Line    Suppression State
Error   CS1061  'IConfigurationBuilder' does not contain a definition for 'AddJsonFile' and no accessible extension method 'AddJsonFile' accepting a first argument of type 'IConfigurationBuilder' could be found (are you missing a using directive or an assembly reference?)    
cs0815
  • 16,751
  • 45
  • 136
  • 299
  • If you inherited this code, it should already be in .Net Core project type, so compile it there. It's not clear as to what you are trying to achieve ultimately. – Vidmantas Blazevicius Dec 20 '19 at 14:01
  • only code some classes which I try to incorporate. Sorry I may have got the bit about Core wrong ... – cs0815 Dec 20 '19 at 14:10

1 Answers1

1

This is .NET Standard code that should work just fine on .NET Framework 4.6.1 or higher.

You don't provide enough information, but I would guess you are missing reference to this NuGet package : https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Json/2.2.0

Or you might be missing proper using:

using Microsoft.Extensions.Configuration;

So that AddJsonFile extension method is found.

Euphoric
  • 12,645
  • 1
  • 30
  • 44