C# – ‘ConfigurationBuilder’ does not contain a definition for ‘AddJsonFile’

.net corec

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();

Best Answer

The AddJsonFile extension method is available in NuGet:

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.

Related Topic