AspNetCore.Docs
AspNetCore.Docs copied to clipboard
WebApplicationFactory example test fails with: The entry point exited without ever building an IHost.
The With WebApplicationFactory example of how to Test with WebApplicationFactory or TestServer doesn't appear to succeed using VS2022 (17.2.6) or Rider 2022.2 EAP 11.
Possibly there's something I'm not following in the docs too, not sure either way.
The following test code based on the example returns:
System.InvalidOperationException : The entry point exited without ever building an IHost.
Example code:
namespace Test
{
public class Tests
{
[Fact]
public async Task HelloWorld()
{
var application = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder => { builder.ConfigureServices(services => { services.AddSingleton<IHelloService, MockHelloService>(); }); });
var client = application.CreateClient();
var response = await client.GetStringAsync("/");
Assert.Equal("Test Hello", response);
}
class MockHelloService : IHelloService
{
public string HelloMessage => "Test Hello";
}
internal interface IHelloService
{
}
}
}
public partial class Program
{
}
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 20acee3e-8b6e-b3fb-8271-661e1a287bdd
- Version Independent ID: 20acee3e-8b6e-b3fb-8271-661e1a287bdd
- Content: Code samples migrated to the new minimal hosting model in 6.0
- Content Source: aspnetcore/migration/50-to-60-samples.md
- Product: aspnet-core
- Technology: aspnetcore-migration
- GitHub Login: @Rick-Anderson
- Microsoft Alias: riande
I'm not sure this is the same as your issue, but I'm finding that anytime you have your Program.cs throw an exception, that exception gets lost (technically you can find it logged as an uncaught exception in console, it seems?), and you get this generic "your code didn't produce an IHost" error with no cause.
You might try removing parts of your Program.cs, one line at a time, to see if you can figure out if any statement you've got there is throwing an exception.
Alternatively, you could wrap all code before builder.Build() in a try-catch, where the catch just logs the error, to help you find what's failing.
Right I found the same thing the real exception is lost and this exception is just telling you something went wrong. I'd expect the example in the docs to work however.
I gave up on the newer ways of starting things up since I ran into too many issues like this when using gRPC. And all the gRPC examples use the old startup style still and work fine.
It sounds like you solved your issue, if not, it will be addressed in #27089
Solved and giving up are a little different imo, but ok :)
I solved problem by adding this line to the bottom of program.cs
public interface IIdentityApiMarker { }
Given that the code in the Program.cs is run in-process you can just set a break point in the Program.cs file to diagnose these issues.