AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

WebApplicationFactory example test fails with: The entry point exited without ever building an IHost.

Open cove opened this issue 3 years ago • 2 comments

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.

cove avatar Aug 02 '22 03:08 cove

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.

jakewins avatar Sep 01 '22 13:09 jakewins

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.

cove avatar Sep 01 '22 13:09 cove

It sounds like you solved your issue, if not, it will be addressed in #27089

Rick-Anderson avatar Sep 30 '22 03:09 Rick-Anderson

Solved and giving up are a little different imo, but ok :)

cove avatar Sep 30 '22 16:09 cove

I solved problem by adding this line to the bottom of program.cs

public interface IIdentityApiMarker { }

jalle007 avatar Jul 26 '23 12:07 jalle007

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.

mh-logiten avatar Sep 13 '23 11:09 mh-logiten