Skip to content
Latchkey

ASP.NET Core Kestrel "Failed to bind to address ... address already in use" in CI

Kestrel could not bind because the TCP port is already held by another process, usually a leftover test host from a prior test or a hardcoded port shared by two parallel jobs. Bind to port 0 or a unique port.

What this error means

The web host throws "Failed to bind to address http://127.0.0.1:5000: address already in use" during startup, often in integration tests that spin up a real Kestrel server.

.NET
System.IO.IOException: Failed to bind to address http://127.0.0.1:5000:
address already in use.
 ---> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use

Common causes

A hardcoded port shared across tests or jobs

Tests bind a fixed port like 5000. Two tests, or two matrix jobs on one runner, collide on the same port.

A leftover host from a previous test

A prior test started Kestrel and did not dispose the host, so the port is still occupied when the next test binds.

How to fix it

Bind to an ephemeral port

Ask the OS for a free port with :0, or let WebApplicationFactory use its in-memory TestServer that needs no real port at all.

Program.cs
builder.WebHost.UseUrls("http://127.0.0.1:0");

Dispose the host between tests

  1. Ensure each test disposes its WebApplicationFactory / host (use IDisposable / IAsyncDisposable).
  2. Prefer the in-memory TestServer over a real Kestrel bind for integration tests.
  3. Avoid a single fixed port shared by parallel test classes.

How to prevent it

  • Use TestServer (in-memory) instead of binding a real port in integration tests.
  • When a real port is needed, request port 0 so the OS assigns a free one.
  • Dispose hosts and factories so ports are released promptly.

Frequently asked questions

What causes ""address already in use""?
Tests bind a fixed port like 5000. Two tests, or two matrix jobs on one runner, collide on the same port.
How do I fix "address already in use"?
Ask the OS for a free port with :0, or let WebApplicationFactory use its in-memory TestServer that needs no real port at all.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card