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

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

Open
1 task done
simkin2004 opened this issue Mar 26, 2025 · 1 comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-mvc-testing MVC testing package

Comments

@simkin2004
Copy link

Is there an existing issue for this?

  • 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<TStartup> : WebApplicationFactory<TStartup> 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<TStartup> : WebApplicationFactory<TStartup> 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<TStartup> : WebApplicationFactory<TStartup> 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 #53999

Additional troubleshooting in the discussion and documented above.

Exceptions (if any)

No response

.NET Version

8.0.302

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Mar 26, 2025
@martincostello martincostello added the feature-mvc-testing MVC testing package label Mar 26, 2025
@simkin2004
Copy link
Author

This potential workaround exhibits the same behavior both with sending the HttpContext (ctx) and not sending it.
Sending the HttpContext (ctx) example is below

var waf = new WebApplicationFactory<Program>()
                        .WithWebHostBuilder(builder =>
                        {
                            builder.Configure(app =>
                            {
                                app.Use(async (ctx, next) => { await next(ctx); });
                            });  
                        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-mvc-testing MVC testing package
Projects
None yet
Development

No branches or pull requests

2 participants