Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

381
Vistas
With WebApplicationFactory, add configuration source before Program.cs executes

I am using the new minimal .NET 6 hosting model, and I have an integration test.

Obviously Program.cs needs configuration values, so I want to use a custom appsettings.Test.json file. Docs say I can use ConfigureAppConfiguration but its delegate runs after Program, hence Program has no configuration. Here's the code added to the Minimal API Playground sample code:

internal class PlaygroundApplication : WebApplicationFactory<Program>
{
    private readonly string _environment;

    public PlaygroundApplication(string environment = "Development")
    {
        _environment = environment;
    }

    protected override IHost CreateHost(IHostBuilder builder)
    {
        builder.UseEnvironment(_environment);

        builder.ConfigureAppConfiguration(config =>
        {
            config.AddJsonFile(appSettings); // runs AFTER Program.cs
        });

        // Add mock/test services to the builder here
        builder.ConfigureServices(services =>
        {
        });

        return base.CreateHost(builder);
    }
}

How can I provide configuration to Program.cs?

var builder = WebApplication.CreateBuilder(args);

var keyVaultName = builder.Configuration["KeyVaultName"]; // null

builder.Configuration.AddAzureKeyVault(new SecretClient(
    new Uri($"https://{keyVaultName}.vault.azure.net/"),
    new DefaultAzureCredential()), new KeyVaultSecretManager());

I can set environment to Testand then in Program do:

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json");

var keyVaultName = builder.Configuration["KeyVaultName"]; // has value from json

builder.Configuration.AddAzureKeyVault(new SecretClient(
    new Uri($"https://{keyVaultName}.vault.azure.net/"),
    new DefaultAzureCredential()), new KeyVaultSecretManager());

But is that correct?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

This is currently not possible with the way the code is written unfortunately, according to https://github.com/dotnet/aspnetcore/issues/37680.

over 4 years ago · Santiago Trujillo Denunciar

0

I found a workaround that I detail here: https://github.com/dotnet/aspnetcore/issues/37680#issuecomment-1032922656

If you override WebApplicationFactory<T>.CreateHost() and call IHostBuilder.ConfigureHostConfiguration() before calling base.CreateHost() the configuration you add will be visible inbetween WebApplication.CreateBuilder() and builder.Build(). From my testing it's visible in all the various places you'd look at the config (callbacks, etc.).

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda