How to access object initialized by ConfigurationProvider

I have created a custom ConfigurationProvider in order to configure and start up a container from the testcontainers-dotnet package.

I have run into an issue, where I need an HttpClient initialized by the provider available when I set up dependency injection for my tests, but I’m unsure how I can make it accessible.

The process has to happen in this order, steps 2 or 3 could happen outside of the provider if I am able somehow to make either the container or httpClient created in it available to my WebApplicationFactory.

  1. Provider configures and starts up TestContainer
  2. HttpClient is created from the started container
  3. HttpClient is injected as a dependency

As an example of my provider approach working well, this is a stripped down version of the basic configuration for the WireMock module.
I’m able start the container and then send the mappings to the WireMock client that will be used in the tests. I can also utilise the Set() function, inherited from ConfigurationProvider, to override values that would normally come from my appsettings file.

public class WireMockConfigurationSource : IConfigurationSource
{
    public IList<MappingModel> Mappings { get; }

    public WireMockConfigurationSource(IList<MappingModel> mappings)
    {
        Mappings = mappings;
    }

    public IConfigurationProvider Build(IConfigurationBuilder builder) => new WireMockConfigurationProvider(this);
}

The bulk of the work happens in the ConfigurationProvider (IDisposable code ommited)

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

    private readonly WireMockContainer _container;
    private readonly WireMockConfigurationSource _source;
    private CancellationTokenSource? _cts;

    public WireMockConfigurationProvider(WireMockConfigurationSource source)
    {
        _source = source;
        _container = new WireMockContainerBuilder()
            .WithAutoRemove(true)
            .WithCleanUp(true)
            .Build();
    }

    public override void Load()
    {
        if (_cts is not null)
        {
            return;
        }

        _cts = new CancellationTokenSource();

        TaskFactory.StartNew(() => LoadAsync(_cts.Token), _cts.Token).Unwrap().ConfigureAwait(false)
            .GetAwaiter()
            .GetResult();
    }

    public async Task LoadAsync(CancellationToken ct)
    {
        // My container is started
        await _container.StartAsync(ct);

        // The URL for the container provided to my settings
        SetAppSettingsForWireMockedComponent();

        // Some data is set up on the container for tests
        await InitializeMappings(ct);
    }

    private async Task InitializeMappings(CancellationToken ct)
    {
        var wireMockAdminClient = _container.CreateWireMockAdminClient();
        await wireMockAdminClient.PostMappingsAsync(_source.Mappings, ct);
    }

    private void SetAppSettingsForWireMockedComponent()
    {
        var containerUrl = _container.GetPublicUrl().TrimEnd('/');

        Set("MyAppSetting:ApiUrl", containerUrl);
    }
}

This is then used by ConfigureAppConfiguration from my WebApplicationFactory

public class ApiFactory<TProgram> : WebApplicationFactory<TProgram>
    where TProgram : class
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureAppConfiguration(configure => configure.Add(new WireMockConfigurationSource()));

        builder.ConfigureServices(
            (context, services) =>
            {
                // DI for test run
                // This is where I would have to inject my HttpClient
            });
    }
}

The would like to reuse the same approach for the CosmosDB module. However, I need to be able to access the WireMockContainer that’s started up in the provider outside of the ConfigurationProvider. This is because the container creates a HttpClient with a trusted certificate, so you can send authorized requests to the CosmosDB instance,

    var cosmosClient = new CosmosClient(
        // connection string for the emulator, this can be passed to app settings via "Set()" so no issues accessing the value outside of the Provider.
        _container.GetConnectionString(),
        new CosmosClientOptions
        {
            // The HTTP client created from the container automatically trusts the emulators certificate. 
            // Without this HttpClient, you get 401 denied when sending requests to CosmosDB.
            // It will be needed outside of the provider to create a CosmosClient for dependency injection
            HttpClientFactory = () => _container.HttpClient,
            ConnectionMode = ConnectionMode.Gateway,
        });

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật