title: WebApi in-memory testing with multiple Web API projects
body: <p>I am using the System.Web.Http.HttpServer to spin up an in-memory server for use in integration testing of my Web API projects. </p>
  
  <p>I've had a single Web API project, and a single Integration Test project in the solution. And this has worked fine. </p>
  
  <p>Now I have added another Web API project to the solution, and when I add test for the new API controllers - then the test fails if test for both Web API projects are run in same test run. </p>
  
  <p>If I run tests for API 'A' - they are all green.
  If I run tests for API 'B' - they are all green.
  If I run a test for API 'A' and a test for API 'B' in same testrun - the second test fails. </p>
  
  <p>So somehow the configuration/settings bleed into each other - I just can't figure out why - and most important how to stop it. </p>
  
  <p>I've read elsewhere that Web API finds the attribute routing by reflection and loading the assemblies - could this be what is happening - that both API's are in memory - and thus the routing fails? The second tests that fail gets an 404 answer back. </p>
  
  <p>My tests are constructed like below</p>
  
  <pre><code>using (var config = new HttpConfiguration())
  {
       config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
  
       ServiceA.AutoMapperConfig.Configure();
       ServiceA.WebApiConfig.Register(config, ImInvoiceService.ChooseAuthorizeFilter.Basic, addPerRequestHandler: false);
  
       using (var server = new HttpServer(config))
       { 
            var client = new HttpClient(server);
            var request = CreateWebRequest(HttpMethod.Post, "api/invoices");
            request.Content = new     StringContent(CreateJsonInvoice(purchaseOrder, overrideCurrencyCode: "XXX"), Encoding.UTF8, JsonContentType);
  
            var response = client.SendAsync(request).Result;
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
       }
   }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>using (var config = new HttpConfiguration())
  {
      config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
  
      ServiceB.AutoMapperConfig.Configure();
      ServiceB.WebApiConfig.Register(config);
  
      using (var server = new HttpServer(config))
      {
  
          var client = new HttpClient(server);
          var request = CreateWebRequest(HttpMethod.Post, "api/invoices");
  
          request.Content = new StringContent(CreateJsonInvoice(purchaseOrder, overrideCurrencyCode: "XXX"), Encoding.UTF8, JsonContentType);
  
          var response = client.SendAsync(request).Result;
          Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
      }
  }
  </code></pre>
  
  <p>As shown in code, I create a new HttpServer, and a new HttpConfiguration in each test - leaving simple me thinking I get new objects. </p>
  
  <p>I'm pretty stuck after trying to debug my way through this, I've tried to clear Routes, Config settings etc, but nothing seems to help. </p>
  
  <p>I could create a new test project for the second API, but as this is integration tests, I depend of some setup - and will need to copy a lot of work to the other test project - and I would like to avoid that. </p>
  
  <p>But if Web API/HttpServer can't handle two projects in same AppDomain that might be my last out. </p>
  
  <p>Any good suggestions are welcome - I hope someone has a good idea. </p>
  
  <p>best regards
  /Anders</p>