Addenvironmentvariables not found

In this Azure tutorial, we will discuss how to fix the error. Configurationbuilder does not contain a definition for addenvironmentvariables. I got it while working with the Azure function to access the app settings using Visual Studio.

Addenvironmentvariables not found

I was working with the Azure Functions and was trying to access application settings using Visual Studio, but 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

configurationbuilder does not contain a 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?)

Addenvironmentvariables not found [Solved]

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

1. Right-click on the Project and click on the Manage NuGet Packages

configurationbuilder addenvironmentvariables not found

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

iconfigurationbuilder does not contain a definition for addenvironmentvariables

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

iconfigurationbuilder addenvironmentvariables

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.

method not found: 'microsoft.extensions.configuration.iconfigurationbuilder microsoft.azure.webjobs.hosting.iwebjobsconfigurationbuilder.get_configurationbuilder()

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

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

system.missingmethodexception: 'method not found: 'microsoft.extensions.configuration.iconfigurationbuilder microsoft.azure.webjobs.hosting.iwebjobsconfigurationbuilder.get_configurationbuilder()'.'

Also, the package below

method not found: 'microsoft.extensions.configuration.iconfigurationbuilder microsoft.azure.webjobs.hosting.iwebjobsconfigurationbuilder.get_configurationbuilder()'.

You may also like following the below Articles

Wrapping Up

In this article, we have discussed how to fix the error configurationbuilder addenvironmentvariables not found. The above solution will also fix the issues

I hope it will help you to fix your issue !!!