Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement]: Support ConfigurationProvider (ASP.NET integration) together with modules #1068

Open
HofmeisterAn opened this issue Dec 7, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@HofmeisterAn
Copy link
Collaborator

Problem

There are several ways to integrate Testcontainers into ASP.NET (integration) tests. Developers often have to write the code to leverage Testcontainers into ASP.NET (integration) tests repeatedly.

Solution

To simplify the integration of dependent services into ASP.NET applications using Testcontainers, we can utilize Microsoft's IConfigurationSource interface and the ConfigurationProvider class. These allow us to initiate default module configurations and set up the actual ASP.NET application, making it straightforward for developers to incorporate Testcontainers into their ASP.NET integration tests and set up their ASP.NET configuration with the dependent services.

private sealed class CustomWebApplicationFactory : WebApplicationFactory<Program>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureAppConfiguration(configure =>
        {
            configure.Add(new RedisConfigurationSource());
        });
    }
}

private sealed class RedisConfigurationSource : IConfigurationSource
{
    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return new RedisConfigurationProvider();
    }
}

private sealed class RedisConfigurationProvider : ConfigurationProvider
{
    private static readonly TaskFactory TaskFactory = new TaskFactory(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default);

    public override void Load()
    {
        // Until the asynchronous configuration provider is available,
        // we use the TaskFactory to spin up a new task that handles the work:
        // https://github.com/dotnet/runtime/issues/79193
        // https://github.com/dotnet/runtime/issues/36018
        TaskFactory.StartNew(LoadAsync)
            .Unwrap()
            .ConfigureAwait(false)
            .GetAwaiter()
            .GetResult();
    }

    public async Task LoadAsync()
    {
        var redisContainer = new RedisBuilder().Build();

        await redisContainer.StartAsync()
            .ConfigureAwait(false);

        Set("ConnectionStrings:RedisCache", redisContainer.GetConnectionString());
    }
}

The interesting part here is the LoadAsync() member that starts the dependent service and sets the connection string. The actual app can simply read the connection string as it usually does using Configuration.GetConnectionString("RedisCache").

I am still considering the best place to implement and store the interface and class. Overall, I aim to avoid implementing them in every module and introducing extra dependencies like Microsoft.Extensions.Configuration to the modules.

Benefit

Developers will be able to integrate Testcontainers into tests much more simply. Leveraging it into ASP.NET (integration) tests would only require a single line and makes the startup and teardown implementation obsolete.

builder.ConfigureAppConfiguration(configure => configure.Add(new RedisConfigurationSource()));

Alternatives

-

Would you like to help contributing this enhancement?

Yes

@HofmeisterAn HofmeisterAn added the enhancement New feature or request label Dec 7, 2023
@HofmeisterAn HofmeisterAn changed the title [Enhancement]: Support ConfigurationProvider together with modules [Enhancement]: Support ConfigurationProvider (ASP.NET integration) together with modules Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant