Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'ConfigurationBuilder' does not contain a definition for 'AddJsonFile'

Tags:

c#

.net-core

I have the following error:

Program.cs(15,72): error CS1061: 'ConfigurationBuilder' does not contain a definition for 'AddJsonFile' and no accessible extension method 'AddJsonFile' accepting a first argument of type 'ConfigurationBuilder' could be found (are you missing a using directive or an assembly

The project is a dotnet core app, console application, using Azure Search SDK

The line of error is below:

using System; using System.Linq; using System.Threading; using Microsoft.Azure.Search; using Microsoft.Azure.Search.Models; using Microsoft.Extensions.Configuration; using Microsoft.Spatial;  namespace DemoSearchIndexer {     class Program     {         static void Main(string[] args)         {             IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");             IConfigurationRoot configuration = builder.Build(); 
like image 789
Luis Valencia Avatar asked Jul 23 '19 07:07

Luis Valencia


People also ask

Does not contain a definition for SetBasePath?

Fix: 'ConfigurationBuilder' does not contain a definition for 'SetBasePath' To fix this error, you will have to install a new NuGet package Microsoft. Extensions. Configuration. Json .

How do I get value from IConfiguration?

Using IConfiguration The IConfiguration is available in the dependency injection (DI) container, so you can directly access JSON properties by simply injecting IConfiguration in the constructor of a controller or class. It represents a set of key/value application configuration properties.

What is IConfiguration C#?

Get<T>(IConfiguration) Attempts to bind the configuration instance to a new instance of type T. If this configuration section has a value, that will be used.


1 Answers

The AddJsonFile extension method is available in NuGet:

  • Microsoft.Extensions.Configuration.Json

When building an ASP.NET Core application, which usually references Microsoft.AspNetCore.App (or, historically, Microsoft.AspNetCore.All), you get this "for free".

When building a console application, or something that doesn't reference the metapackages, you need an explicit reference to Microsoft.Extensions.Configuration.Json.

like image 148
Kirk Larkin Avatar answered Sep 24 '22 01:09

Kirk Larkin