Error CS1061 ‘IConfigurationBuilder’ does not contain definition for ‘AddEnvironmentVariables’

In this azure tutorial, we will discuss how to fix the error, Error CS1061 ‘IConfigurationBuilder’ does not contain a definition for ‘AddEnvironmentVariables’. This error I got while working with Azure function to access the app settings using the Visual Studio.

Error CS1061 ‘IConfigurationBuilder’ does not contain definition for ‘AddEnvironmentVariables’

I was working with the Azure Functions and was trying to access application settings using Visual Studio, I got the error.

The Azure function was like below

public static class MyProductHTTPFunction
    {
        [FunctionName("MyProductHTTPFunction")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("MyProductHTTPFunction function processed a request.");

            var configurationBuilder = new ConfigurationBuilder()
                .SetBasePath(context.FunctionAppDirectory)
                .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) 
                .AddEnvironmentVariables() 
                .Build();

            string myappsettingsValue = configurationBuilder["appsettingkey"];

You can see here

Error CS1061 IConfigurationBuilder does not contain definition for AddEnvironmentVariables

The exact error message was as below

Error CS1061 ‘IConfigurationBuilder’ does not contain a definition for ‘AddEnvironmentVariables’ and no accessible extension method ‘AddEnvironmentVariables’ accepting a first argument of type ‘IConfigurationBuilder’ could be found (are you missing a using directive or an assembly reference?)

Error CS1061 ‘IConfigurationBuilder’ does not contain definition for ‘AddEnvironmentVariables’ [Solved]

To fix this error, we need to add few NuGet Packages. To do that follow the below steps

Right click on the Project and click on the Manage NuGet Packages

'IConfigurationBuilder' does not contain definition for 'AddEnvironmentVariables'

You need to add the Microsoft.Extensions.Configuration.EnvironmentVariables package like below. Search for the same package and click on the Install button.

Error CS1061 IConfigurationBuilder does not contain a definition for AddEnvironmentVariables and no accessible extension method

Same way, you can add the Nuget package Microsoft.Extensions.Configuration.UserSecrets.

Error CS1061 'IConfigurationBuilder' does not contain a definition

The above are the two packages you need to install, if you are getting the error due to AddEnvironmentVariables().

There is a chance you might get the same error for the SetBasePath() method. In that case, you need to add one Nuget package in the same way as above i.e Microsoft.Extensions.Configuration.Abstractions.

'IConfigurationBuilder' does not a contain definition

This will fix the issue in case of SetBasePath() method.

Also, You might get the same error for the AddJsonFile() method. To fix the issue you need to install two Nuget Packages that are Microsoft.Extensions.Configuration.FileExtensions and Microsoft.Extensions.Configuration.Json.

Error CS1061 IConfigurationBuilder does not contain definition

And also, the below package

iconfiguration does not contain a definition for getvalue

You may also like following the below Articles

Wrapping Up

In this article, we have discussed how to fix the error Error CS1061 ‘IConfigurationBuilder’ does not contain definition for ‘AddEnvironmentVariables. Hope it will help you to fix your issue !!!

Leave a Comment