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

Code before:
    /// </summary>
    public TestServer Server
    {
        get
        {
            if (_useKestrel)
            {
                throw new NotSupportedException(Resources.TestServerNotSupportedWhenUsingKestrel);
            }

            StartServer();
            return _server!;
        }
    }

    /// <summary>
    /// Gets the <see cref="IServiceProvider"/> created by the server associated with this <see cref="WebApplicationFactory{TEntryPoint}"/>.
    /// </summary>
    public virtual IServiceProvider Services
    {
        get
        {
            StartServer();
            if (_useKestrel)
            {
                return _webHost!.Services;
            }

            return _host?.Services ?? _server!.Host.Services;
        }
    }

    /// <summary>
    /// Helps determine if the `StartServer` method has been called already.
    /// </summary>
    private bool ServerStarted => _webHost != null || _host != null || _server != null;

    /// <summary>
    /// Gets the <see cref="IReadOnlyList{WebApplicationFactory}"/> of factories created from this factory
    /// by further customizing the <see cref="IWebHostBuilder"/> when calling
    /// <see cref="WebApplicationFactory{TEntryPoint}.WithWebHostBuilder(Action{IWebHostBuilder})"/>.
    /// </summary>
    public IReadOnlyList<WebApplicationFactory<TEntryPoint>> Factories => _derivedFactories.AsReadOnly();

    /// <summary>
    /// Gets the <see cref="WebApplicationFactoryClientOptions"/> used by <see cref="CreateClient()"/>.
    /// </summary>
    public WebApplicationFactoryClientOptions ClientOptions { get; private set; } = new WebApplicationFactoryClientOptions();

    /// <summary>
    /// Creates a new <see cref="WebApplicationFactory{TEntryPoint}"/> with a <see cref="IWebHostBuilder"/>

Review comment (suggestion):
Ensure that _host is guaranteed to be non-null when _webHost is null to avoid a potential NullReferenceException in future scenarios.
```suggestion
                if (_webHost != null)
                {
                    return _webHost.Services;
                }

                if (_host == null)
                {
                    throw new InvalidOperationException("Both _webHost and _host are null. Ensure that StartServer initializes one of them.");
                }

                return _host.Services;
```

Code after:
    /// </summary>
    public TestServer Server
    {
        get
        {
            if (_useKestrel)
            {
                throw new NotSupportedException(Resources.TestServerNotSupportedWhenUsingKestrel);
            }

            StartServer();
            return _server!;
        }
    }

    /// <summary>
    /// Gets the <see cref="IServiceProvider"/> created by the server associated with this <see cref="WebApplicationFactory{TEntryPoint}"/>.
    /// </summary>
    public virtual IServiceProvider Services
    {
        get
        {
            StartServer();
            if (_useKestrel)
            {
                return _webHost?.Services ?? _host!.Services;
            }

            return _host?.Services ?? _server!.Host.Services;
        }
    }

    /// <summary>
    /// Helps determine if the `StartServer` method has been called already.
    /// </summary>
    private bool ServerStarted => _webHost != null || _host != null || _server != null;

    /// <summary>
    /// Gets the <see cref="IReadOnlyList{WebApplicationFactory}"/> of factories created from this factory
    /// by further customizing the <see cref="IWebHostBuilder"/> when calling
    /// <see cref="WebApplicationFactory{TEntryPoint}.WithWebHostBuilder(Action{IWebHostBuilder})"/>.
    /// </summary>
    public IReadOnlyList<WebApplicationFactory<TEntryPoint>> Factories => _derivedFactories.AsReadOnly();

    /// <summary>
    /// Gets the <see cref="WebApplicationFactoryClientOptions"/> used by <see cref="CreateClient()"/>.
    /// </summary>
    public WebApplicationFactoryClientOptions ClientOptions { get; private set; } = new WebApplicationFactoryClientOptions();

    /// <summary>
    /// Creates a new <see cref="WebApplicationFactory{TEntryPoint}"/> with a <see cref="IWebHostBuilder"/>