how to fix the error in AddJsonFile method while making a console application to read data from appsettings file

1.2k Views Asked by At

This is the code I have implemented to check whether the process is running or not.I have reerred .NET Core 3.1 loading config from appsettings.json for console application. but I get an error as the attached below.Even after importing the libraries.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

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

    {
        IConfiguration configuration = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", true, true)
        .Build();
        var processSection = configuration.GetSection(nameof(Process));


    }
  

Iam not sure whether my code under main method is correct or not,what i wanted to do is, I want to read the names of the processes from appsettings file.

Can someone please let me know how can I do it.

Thankyou.

appsettings.json file is as below

{
"Process": {
    "name": "notepad"
    
}

}

the .csproj file is,

  <Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
  </ItemGroup>

</Project>

error I get

1

There are 1 best solutions below

0
Moonwar On

just add a reference to Microsoft.Extensions.Configuration.Json from the NuGet package manager.

Or use this command in package manager console:

dotnet add package Microsoft.Extensions.Configuration.Json --version 6.0.0

I think it will solve your problem