Repository: dotnet/aspnetcore
File: src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs (C#)

Code before:
                nameof(IWebHostBuilder),
                typeof(TEntryPoint).Assembly.EntryPoint!.DeclaringType!.FullName,
                typeof(WebApplicationFactory<TEntryPoint>).Name,
                nameof(CreateHostBuilder),
                nameof(CreateWebHostBuilder)));
        }
        else
        {
            SetContentRoot(builder);
            _configuration(builder);
            if (_useKestrel)
            {
                _webHost = CreateKestrelServer(builder);

                TryExtractHostAddress(() => GetServerAddressFeature(_webHost));
            }
            else
            {
                _server = CreateServer(builder);
            }
        }
    }

    private void TryExtractHostAddress(Func<IServerAddressesFeature?> serverAddressFeatureAccessor)
    {
        var serverAddressFeature = serverAddressFeatureAccessor();
        if (serverAddressFeature?.Addresses.Count > 0)
        {
            // Store the web host address as it's going to be used every time a client is created to communicate to the server
            _webHostAddress = new Uri(serverAddressFeature.Addresses.Last());
            ClientOptions.BaseAddress = _webHostAddress;
        }
    }

    private void ConfigureHostBuilder(IHostBuilder hostBuilder)
    {
        hostBuilder.ConfigureWebHost(webHostBuilder =>
        {
            SetContentRoot(webHostBuilder);
            _configuration(webHostBuilder);
            if (_useKestrel)
            {
                ConfigureBuilderToUseKestrel(webHostBuilder);
            }
            else
            {
                webHostBuilder.UseTestServer();
            }
        });
        _host = CreateHost(hostBuilder);
        if (_useKestrel)

Review comment (refactor):
Nit:

```suggestion
    private void TryExtractHostAddress(IServerAddressesFeature? serverAddressFeature)
    {
```

Of course, the call sites will need to be updated too.

Code after:
                nameof(IWebHostBuilder),
                typeof(TEntryPoint).Assembly.EntryPoint!.DeclaringType!.FullName,
                typeof(WebApplicationFactory<TEntryPoint>).Name,
                nameof(CreateHostBuilder),
                nameof(CreateWebHostBuilder)));
        }
        else
        {
            SetContentRoot(builder);
            _configuration(builder);
            if (_useKestrel)
            {
                _webHost = CreateKestrelServer(builder);

                TryExtractHostAddress(GetServerAddressFeature(_webHost));
            }
            else
            {
                _server = CreateServer(builder);
            }
        }
    }

    private void TryExtractHostAddress(IServerAddressesFeature? serverAddressFeature)
    {
        if (serverAddressFeature?.Addresses.Count > 0)
        {
            // Store the web host address as it's going to be used every time a client is created to communicate to the server
            _webHostAddress = new Uri(serverAddressFeature.Addresses.Last());
            ClientOptions.BaseAddress = _webHostAddress;
        }
    }

    private void ConfigureHostBuilder(IHostBuilder hostBuilder)
    {
        hostBuilder.ConfigureWebHost(webHostBuilder =>
        {
            SetContentRoot(webHostBuilder);
            _configuration(webHostBuilder);
            if (_useKestrel)
            {
                ConfigureBuilderToUseKestrel(webHostBuilder);
            }
            else
            {
                webHostBuilder.UseTestServer();
            }
        });
        _host = CreateHost(hostBuilder);
        if (_useKestrel)
        {