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

Code before:
    {
        var factory = new DelegatedWebApplicationFactory(
            ClientOptions,
            CreateServer,
            CreateHost,
            CreateWebHostBuilder,
            CreateHostBuilder,
            GetTestAssemblies,
            ConfigureClient,
            builder =>
            {
                _configuration(builder);
                configuration(builder);
            });

        _derivedFactories.Add(factory);

        return factory;
    }

    /// <summary>
    /// Configures the factory to use Kestrel as the server.
    /// </summary>
    public void UseKestrel()
    {
        if (_server != null || _webHost != null)
        {
            throw new InvalidOperationException(Resources.UseKestrelCanBeCalledBeforeInitialization);
        }

        _useKestrel = true;
    }

    /// <summary>
    /// Configures the factory to use Kestrel as the server.
    /// </summary>
    /// <param name="port">The port to listen to when the server starts. Use `0` to allow dynamic port selection.</param>
    /// <exception cref="InvalidOperationException">Thrown, if this method is called after the WebHostFactory has been initialized.</exception>
    /// <remarks>This method should be called before the factory is initialized either via one of the <see cref="CreateClient()"/> methods
    /// or via the <see cref="StartServer"/> method.</remarks>
    public void UseKestrel(int port)
    {
        UseKestrel();

        this._kestrelPort = port;
    }

    /// <summary>
    /// Configures the factory to use Kestrel as the server.
    /// </summary>
    /// <param name="configureKestrelOptions">A callback handler that will be used for configuring the server when it starts.</param>

Review comment (suggestion):
```suggestion
        if (ServerStarted)
```

I think this should also throw if `_host` is set.

Code after:
    {
        var factory = new DelegatedWebApplicationFactory(
            ClientOptions,
            CreateServer,
            CreateHost,
            CreateWebHostBuilder,
            CreateHostBuilder,
            GetTestAssemblies,
            ConfigureClient,
            builder =>
            {
                _configuration(builder);
                configuration(builder);
            });

        _derivedFactories.Add(factory);

        return factory;
    }

    /// <summary>
    /// Configures the factory to use Kestrel as the server.
    /// </summary>
    public void UseKestrel()
    {
        if (ServerStarted)
        {
            throw new InvalidOperationException(Resources.UseKestrelCanBeCalledBeforeInitialization);
        }

        _useKestrel = true;
    }

    /// <summary>
    /// Configures the factory to use Kestrel as the server.
    /// </summary>
    /// <param name="port">The port to listen to when the server starts. Use `0` to allow dynamic port selection.</param>
    /// <exception cref="InvalidOperationException">Thrown, if this method is called after the WebHostFactory has been initialized.</exception>
    /// <remarks>This method should be called before the factory is initialized either via one of the <see cref="CreateClient()"/> methods
    /// or via the <see cref="StartServer"/> method.</remarks>
    public void UseKestrel(int port)
    {
        UseKestrel();

        this._kestrelPort = port;
    }

    /// <summary>
    /// Configures the factory to use Kestrel as the server.
    /// </summary>
    /// <param name="configureKestrelOptions">A callback handler that will be used for configuring the server when it starts.</param>