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();
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 .
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With