1

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

  • If you have installed the nuget packages - have you updated the `using` statements to reference them? – Fermin Jul 29 '21 at 09:52
  • Install the [NuGet package](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Json/) and be sure to have a `using Microsoft.Extensions.Configuration;` – ProgrammingLlama Jul 29 '21 at 09:52
  • @Llama I have already imported using Microsoft.Extensions.Configuration;. But still I get the error.I updated the question with the current code –  Jul 29 '21 at 09:56
  • 1
    Please add a [mcve]. I just created a new .NET 5 console application, added the Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.Json NuGet packages, pasted your code, and [as you can see](https://i.stack.imgur.com/nPkum.png) it compiles just fine. I would suggest you also include your .csproj file. – ProgrammingLlama Jul 29 '21 at 10:01
  • @Llama iam using .NET 2.2. then only i get the error as I have update above. –  Jul 29 '21 at 10:20
  • @Llama I want to read the names of the processes from appsettings file.But iam not sure whether my code under main method is correct for the requirment. –  Jul 29 '21 at 10:21
  • 2.2 is fine too: https://i.stack.imgur.com/ABkRe.png – ProgrammingLlama Jul 29 '21 at 10:22
  • @Llama I want to read the names of the processes from appsettings file.But iam not sure whether my code under main method is correct for the requirment. –  Jul 29 '21 at 10:23
  • The code looks fine to me: it will read the entire section called "Process". – ProgrammingLlama Jul 29 '21 at 10:24
  • @Fermin yes i have. I want to read the names of the processes from appsettings file.But iam not sure whether my code under main method is correct for the requirment. –  Jul 29 '21 at 10:24
  • Please provide a [mcve] for _this question_ before asking new questions. – ProgrammingLlama Jul 29 '21 at 10:25
  • @Llama I provided.And also i provided the snap of the error at the end of the code.Can u please check –  Jul 29 '21 at 10:44
  • No, a [mcve] allows us to reproduce your exact issue. I've demonstrated twice now that your code works absolutely fine. I've also prompted you to include your .csproj file, but you've ignored this request. The ball is truly in your court for getting help with this, but we can only help you if you help us to do so. – ProgrammingLlama Jul 29 '21 at 10:57
  • @Llama, I attached the .csproj file as well.Sorry I didnt ignore,I am new to c#,so i was unable to figure it out what you were asking.You had asked to provide minimal reproducible code,and i understood it as you are asking to provide the piece of code where i get the error only.Thatswhy I updated as earlier.If its wrong,iam sorry i cant understand what minimal reproducible code is,even after reading the article. –  Jul 29 '21 at 11:13
  • From the image it looks like you have not installed the "Microsoft.Extensions.Configuration.Json" package. Try adding the package `Install-Package Microsoft.Extensions.Configuration.Json -Version 5.0.0` – Nitheesh Govind Jul 29 '21 at 11:43
  • A [mcve] is basically the shortest amount of code (and other information, as required) to reproduce the issue being asked about. In this case, the code you provided doesn't reproduce the issue when the correct packages are installed. The .csproj file contains, amongst other things, the packages installed within a project. Had the contents of that file been provided, it would have been clear that the Microsoft.Extensions.Configuration.Json package isn't installed. – ProgrammingLlama Jul 29 '21 at 12:33
  • @Llama Now there are no errors but the value from appsettings json class (name of the process) is not passing to var processSection = configuration.GetSection(nameof(Process)); how can I use the value in the appsettings.json file?my appsetting.json file looks as above in the question. –  Jul 29 '21 at 12:45
  • This is definitely a new question you should ask. I don't see any obvious reason why it shouldn't be loaded though. – ProgrammingLlama Jul 29 '21 at 13:04
  • @Llama, yes i did.https://stackoverflow.com/questions/68576457/how-to-fix-the-error-the-name-processrunning-does-not-exist-in-the-current-co?noredirect=1#comment121194829_68576457. Can you please help me if possible.Thankyou. –  Jul 29 '21 at 15:46

1 Answers1

0

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

NPras
  • 3,135
  • 15
  • 29
Moonwar
  • 31
  • 1
  • 4