title: Controllers Not Mapping on WebApplicationFactory for TestClient .NET 6
body: <p>So I'm upgrading a bunch of our APIs and migrating them to the new shiny top-level statement style of startup.</p>
  <p>These projects all also have integration tests that rely upon a <code>WebApplicationFactory</code> to create a <code>TestClient</code> for them. For the most-part, retargetting that at <code>Program</code> rather than <code>Startup</code> has worked just fine.</p>
  <p>However, on one of my APIs, I just get 404s whenever I try to call a controller and I can't for the life of me work out why.</p>
  <p>If I add in a minimal <code>app.MapGet(&quot;test&quot;, () =&gt; &quot;test&quot;)</code> I can hit that from the test, so I guess for some reason, the controllers are getting unmapped. That being said, I'm struggling to see much difference between this API and the other ones.</p>
  <p>So, all of my APIs follow the same basic pattern in <code>Program</code>:</p>
  <pre class="lang-cs prettyprint-override"><code>var builder = WebApplication.CreateBuilder();
  
  BuildConfiguration();
  ConfigureLogging();
  ConfigureXRay();
  ConfigureServices();
  ConfigureHost();
  
  var app = builder.Build();
  ConfigureApp();
  
  app.Run();
  </code></pre>
  <p>I haven't included everything for brevity, but in <code>ConfigureServices()</code> I believe this line should add and configure all of the controllers:</p>
  <pre class="lang-cs prettyprint-override"><code>void ConfigureServices()
  {
      builder.Services.ConfigureHealthChecks();
      builder.Services.ConfigureAppMetrics();
  
      builder.Services.ConfigureFromAppSettingsConfig(builder.Configuration);
      builder.Services.ConfigureIOCServices(builder.Configuration);
  
      builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
      builder.Services.AddControllers(opt =&gt; opt.AddGlobalErrorHandling()); //&lt;----- This one
      builder.Services.AddSoapCore();
  
      builder.Services.ConfigureSwagger();
  }
  </code></pre>
  <p>Then in <code>ConfigureApp()</code> I've got this call which I believe should map the controllers:</p>
  <pre class="lang-cs prettyprint-override"><code>    app.UseEndpoints(endpoints =&gt;
                       {
                           endpoints.MapControllers();
                           endpoints.UseSoapEndpoint&lt;ICorporateActionsSoapService&gt;(&quot;/investments/api/CorporateActions.asmx&quot;, new BasicHttpBinding());
                       });
  </code></pre>
  <p>So if I run the app directly, all of that seems to work. For my tests, I've got the following.</p>
  <pre><code>public class CustomWebApplicationFactory : WebApplicationFactory&lt;Program&gt;
  {
      public CustomWebApplicationFactory()
      {
          SetAppSettingsValues();
          DisableMetricsBuiltFlag();
      }
  
      private void DisableMetricsBuiltFlag()
      {
          var metricsBuiltField = typeof(MetricsAspNetHostBuilderExtensions).GetField(&quot;_metricsBuilt&quot;,
                                                                                      BindingFlags.Static |
                                                                                      BindingFlags.NonPublic);
          metricsBuiltField?.SetValue(null, false);
      }
  
      private void SetAppSettingsValues()
      {
          Environment.SetEnvironmentVariable(&quot;IntegrationTests&quot;, &quot;true&quot;);
      }
  }
  </code></pre>
  <p>And then this is injected into my test classes as an <code>IClassFixture&lt;CustomWebApplicationFactory&gt;</code> by XUnit. I then replace a few of the services with mocks and create a test client.</p>
  <pre class="lang-cs prettyprint-override"><code>public GlobalErrorHandlingIntegrationTest(CustomWebApplicationFactory factory)
  {
      _mockLogger       = _fixture.Create&lt;ILogger&lt;CorporateActionsController&gt;&gt;();
      _mockAuditService = _fixture.Create&lt;ICorporateActionsEmailAuditService&gt;();
  
      _mockEmailService = _fixture.Create&lt;ICorporateActionsEmailService&gt;();
      _mockEmailService.GetEmails().Returns(Task.Run(() =&gt; _fixture.Create&lt;CorporateActionResponse&gt;()));
      _appFactory = factory
         .WithWebHostBuilder
             (
              builder =&gt;
               {
                  builder.ConfigureTestServices
                      (
                       x =&gt;
                       {
                            x.Add(new ServiceDescriptor(typeof(ILogger&lt;CorporateActionsController&gt;), _mockLogger));
                            x.Add(new ServiceDescriptor(typeof(ICorporateActionsEmailService),       _mockEmailService));
                            x.Add(new ServiceDescriptor(typeof(ICorporateActionsEmailAuditService),  _mockAuditService));
                        }
                       );
               }
              );
     _testClient = _appFactory.CreateClient();
  }
  </code></pre>
  <p>And then all of my tests using that <code>_testClient</code> just 404. Any idea what's going wrong here and why these controllers might be getting unmapped?</p>
  <hr />
  <p>I've seen <a href="https://stackoverflow.com/questions/70299688/webapplicationfactory-always-return-404-for-controllers-calls-in-net-core-6">this question</a> but the answer doesn't seem to apply to my case.</p>