Skip to content

WebApplication does not observe changes made with WebApplicationFactory<T> in tests #33876

Closed
@martincostello

Description

@martincostello

Describe the bug

When using WebApplicationFactory/WebApplication in conjunction with WebApplicationFactory<T> for integration tests, the WebApplication created by WebApplicationFactory does not observe configuration changes made to the IWebHostBuilder by WebApplicationFactory<T>.

First, if the configuration is changed using ConfigureAppConfiguration(), the changes are not visible in the IConfiguration resolved by the WebApplication. This applies to adding new sources as well as clearing the existing sources. In both cases, the original configuration remains with the built application, and the changes applied for the tests by the WebApplicationFactory<T> are not visible to the application.

Calling serviceProvider.GetServices<IConfiguration>().Count() within the application returns a value of 2. It appears that both two IConfiguration implementations, one for the web host and another for WebApplication are registered with the service provider, with the WebApplication one being resolved by calls to GetService<IConfiguration>() meaning that the changes are not observed.

This can be worked around by using ConfigureServices() to clear the duplicate registration to leave the one required by the tests:

builder.ConfigureServices(services =>
{
    var descriptors = services.Where(p => p.ServiceType == typeof(IConfiguration)).ToList();
    services.Remove(descriptors[1]);
});

Secondly, changes to the content root are not observed by the application. If UseContentRoot() is used to change the directory to serve content by WebApplicationFactory<T>, the change is not visible to the WebApplication instance, which continues to use the path associated with where the test process was launched from.

This causes static content to not be found when requested.

To Reproduce

An application that reproduces this issue is available at https://github.com/martincostello/WebApplicationFactory-Config-Issues. To reproduce the issue:

  1. Clone https://github.com/martincostello/WebApplicationFactory-Config-Issues.git
  2. Run build.ps1

The following test failures are then observed:

  1. The Can_Override_Configuration test fails because the original value of Value is used from configuration ("a") instead of the one overridden by the tests ("b").
  2. The Can_Override_ContentRoot tests fails because a 404 is returned due to the application being unable to find the test.txt file to serve the static content.
  3. The Can_Remove_Configuration test fails because the original value of Value is used from configuration ("a") instead of an empty value of "" because all of the configuration sources were removed.

Running the application under the debugger in Visual Studio 2022 and requesting the /api or /test.txt resources in a browser show the application works as expected when run outside of the test project.

Similarly the Application_Returns_Defaults test also works correctly as no overrides are provided.

Further technical details

  • .NET SDK: 6.0.100-preview.6.21324.1 (I tried to use the latest preview 7 build but had issues running it in Visual Studio 2022)
  • Visual Studio 2022 Preview 1.1

Metadata

Metadata

Assignees

Labels

Priority:0Work that we can't release withoutarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcbugThis issue describes a behavior which is not expected - a bug.feature-minimal-hosting

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions