Issue: AspNetCoreSelfHost.IsStarted set before the server actually started for real #140

Closed
opened 2025-12-29 08:22:45 +01:00 by adam · 0 comments
Owner

Originally created by @davide-romanini on GitHub (Sep 2, 2018).

From this code:

2075589e98/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs (L107-L117)

is clear that IsStarted is set to true before the WebHost istance is running for real.

That causes sporadic "Connection refused" failures with code like this:

  var mockServer = FluentMockServer.Start();
  // we expect the server to be actually running and listening, but sometimes it's not still ready!
  await new HttpClient().SendAsync(new HttpRequestMessage(HttpMethod.Head, $"http://localhost:{mockServer.Ports[0]}"));

A better implementation could be listening for IApplicationLifetime events:

    IApplicationLifetime appLifeTime = (IApplicationLifetime)_host.Services.GetService(typeof(IApplicationLifetime));
    appLifeTime.ApplicationStarted.Register(() => {
        IsStarted = true;
    });
Originally created by @davide-romanini on GitHub (Sep 2, 2018). From this code: https://github.com/WireMock-Net/WireMock.Net/blob/2075589e987c863c99bfcebe3c8692c5d678e510/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs#L107-L117 is clear that `IsStarted` is set to true *before* the `WebHost` istance is running for real. That causes sporadic "Connection refused" failures with code like this: ``` var mockServer = FluentMockServer.Start(); // we expect the server to be actually running and listening, but sometimes it's not still ready! await new HttpClient().SendAsync(new HttpRequestMessage(HttpMethod.Head, $"http://localhost:{mockServer.Ports[0]}")); ``` A better implementation could be listening for `IApplicationLifetime` events: ``` IApplicationLifetime appLifeTime = (IApplicationLifetime)_host.Services.GetService(typeof(IApplicationLifetime)); appLifeTime.ApplicationStarted.Register(() => { IsStarted = true; }); ```
adam added the bug label 2025-12-29 08:22:45 +01:00
adam closed this issue 2025-12-29 08:22:46 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#140