dotnet / dotnet/aspnetcore

WebApplicationFactory<TEntryPoint> will not allow Middleware to be added via the ConfigureWebHost() method and maintain the existing functionality from the TEntryPoint.

Open
#61,170 1 comment 1 reaction 0 assignees View on GitHub
area-mvc feature-mvc-testing
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

When attempting to use a CustomWebApplicationFactory, I wanted to add a Middleware to set some specific values to handle underlying HttpContext.
Started with this code returning a 200 - OK
```
public class CustomWebApplicationFactory : WebApplicationFactory where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
}
}

```

I tried to jump to the simple middleware to ensure this would work with this code and it returned a 404 - Not Found

```
public class CustomWebApplicationFactory : WebApplicationFactory where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{

builder.Configure(app =>
{
app.Use(async (ctx, next) => { await next(ctx); });

});
base.ConfigureWebHost(builder);
}
}
```

Someone in discussions suggested that I didn't need to pass the context, so i tried this code and it returned a 404 - Not Found
```
public class CustomWebApplicationFactory : WebApplicationFactory where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{

builder.Configure(app =>
{
app.Use(async (ctx, next) => { await next(); });

});
base.ConfigureWebHost(builder);
}
}
```

Ultimately, I tried to do a "No Operation" call to see if my code was breaking it; this code returned a 404 - Not Found.

protected override void ConfigureWebHost(IWebHostBuilder builder)
{

builder.Configure(app =>
{
// no op
});
base.ConfigureWebHost(builder);
}
}

### Expected Behavior

Expected behavior is that the middleware configurations above would operate and return a 200 - OK by relying on the configuration provided by the TEntryPoint.

### Steps To Reproduce

simple sample project provided by https://github.com/dotnet/aspnetcore/discussions/53999

Additional troubleshooting in the discussion and documented above.

### Exceptions (if any)

_No response_

### .NET Version

8.0.302

### Anything else?

_No response_

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.