My environment variables are not recognized in Azure - ASP.NET Core MVC

78 views Asked by At

I have a project in ASP.NET Core with a database, I published it through GitHub Actions and created a Web Service App. I configured my environment variable with the Environment.GetEnvironmentVariable method.

I created my environment variable on my machine and added it to Azure under "Environment variables". When I run it on my machine, the variable is recognized and connects normally to the database, but in Azure it says that it is not defined, even though I have already saved it. Does anyone know what the reason for this might be?

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        // Add services to the container.
        builder.Services.AddControllersWithViews();
        var provider = builder.Services.BuildServiceProvider();
        var configuration = provider.GetRequiredService<IConfiguration>();

        var connString = Environment.GetEnvironmentVariable("DATABASE_CONNECTION_STRING");

        if (string.IsNullOrEmpty(connString))
        {
            throw new InvalidOperationException("A variável de ambiente DATABASE_CONNECTION_STRING não está definida.");
        }
    }
}

Appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

I've looked for information and haven't found anything that helps me.

0

There are 0 answers