dotnet / dotnet/docs

WebHostBuilder().UseStartup<Startup> cannot read appsittings.json by default

Open
#24,529 0 comments 0 reactions 0 assignees View on GitHub
dotnet-architecture/svc microservices/subsvc okr-quality
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

On your document, Under [Implementing integration and functional tests](https://docs.microsoft.com/en-us/dotnet/architecture/microservices/multi-container-microservice-net-applications/test-aspnet-core-services-web-apps#implementing-integration-and-functional-tests-for-each-microservice) you mentioned the following code:

```
public class PrimeWebDefaultRequestShould
{
private readonly TestServer _server;
private readonly HttpClient _client;

public PrimeWebDefaultRequestShould()
{
// Arrange
_server = new TestServer(new WebHostBuilder()
.UseStartup());
_client = _server.CreateClient();
}

[Fact]
public async Task ReturnHelloWorld()
{
// Act
var response = await _client.GetAsync("/");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal("Hello World!", responseString);
}
}

```

and by using this code, the `TestServer` will initialize a `startup` that has no access to the appsitings.json, and an exception of NullReferenceException will be raised in any code that tries to read configuration from appsitting.json.

so please fix the code to detect `appsitting`, and if you mentioned how to select an environment this will be appreciated.

PS: I fixed this by using `new WebApplicationFactory()` rather than `new TestServer(new WebHostBuilder().UseStartup());`

Regards.
---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: 245ea389-75e9-1321-3be4-dc8d8e47a925
* Version Independent ID: 1db547e8-a0b0-cf44-dba4-b65786c576fe
* Content: [Testing ASP.NET Core services and web apps](https://docs.microsoft.com/en-us/dotnet/architecture/microservices/multi-container-microservice-net-applications/test-aspnet-core-services-web-apps)
* Content Source: [docs/architecture/microservices/multi-container-microservice-net-applications/test-aspnet-core-services-web-apps.md](https://github.com/dotnet/docs/blob/main/docs/architecture/microservices/multi-container-microservice-net-applications/test-aspnet-core-services-web-apps.md)
* Product: **dotnet-architecture**
* Technology: **microservices**
* GitHub Login: @nishanil
* Microsoft Alias: **nanil**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.