Repository: dotnet/aspnetcore
File: src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs (C#)

Code before:
        if (_httpContext is null)
        {
            _httpContext = httpContext;
        }
        else if (_httpContext != httpContext)
        {
            throw new InvalidOperationException("The HttpContext cannot change value once assigned.");
        }
    }

    internal async Task InitializeStandardComponentServicesAsync(
        HttpContext httpContext,
        [DynamicallyAccessedMembers(Component)] Type? componentType = null,
        string? handler = null,
        IFormCollection? form = null)
    {
        var navigationManager = (IHostEnvironmentNavigationManager)httpContext.RequestServices.GetRequiredService<NavigationManager>();
        navigationManager?.Initialize(GetContextBaseUri(httpContext.Request), GetFullUri(httpContext.Request));

        if (navigationManager is HttpNavigationManager httpNavigationManager)
        {
            httpNavigationManager.NotFoundEvent += (sender, args) =>
            {
                SetNotFoundResponse();
            };
        }

        var authenticationStateProvider = httpContext.RequestServices.GetService<AuthenticationStateProvider>();
        if (authenticationStateProvider is IHostEnvironmentAuthenticationStateProvider hostEnvironmentAuthenticationStateProvider)
        {
            var authenticationState = new AuthenticationState(httpContext.User);
            hostEnvironmentAuthenticationStateProvider.SetAuthenticationState(Task.FromResult(authenticationState));
        }

        if (authenticationStateProvider != null)
        {
            var authStateListeners = httpContext.RequestServices.GetServices<IHostEnvironmentAuthenticationStateProvider>();
            Task<AuthenticationState>? authStateTask = null;
            foreach (var authStateListener in authStateListeners)
            {
                authStateTask ??= authenticationStateProvider.GetAuthenticationStateAsync();
                authStateListener.SetAuthenticationState(authStateTask);
            }
        }

        InitializeResourceCollection(httpContext);

        if (handler != null && form != null)
        {
            httpContext.RequestServices.GetRequiredService<HttpContextFormDataProvider>()
                .SetFormData(handler, new FormCollectionReadOnlyDictionary(form), form.Files);

Review comment (bug):
I don't think we want the `if` check here. `AddInteractiveServerComponents` will register a `RemoteNavigationManager` that will make this cast fail.

The `NotFound` event is directly available in `NavigationManager`, so could we simply subscribe unconditionally instead?

Code after:
    private void SetHttpContext(HttpContext httpContext)
    {
        if (_httpContext is null)
        {
            _httpContext = httpContext;
        }
        else if (_httpContext != httpContext)
        {
            throw new InvalidOperationException("The HttpContext cannot change value once assigned.");
        }
    }

    internal async Task InitializeStandardComponentServicesAsync(
        HttpContext httpContext,
        [DynamicallyAccessedMembers(Component)] Type? componentType = null,
        string? handler = null,
        IFormCollection? form = null)
    {
        var navigationManager = httpContext.RequestServices.GetRequiredService<NavigationManager>();
        ((IHostEnvironmentNavigationManager)navigationManager)?.Initialize(GetContextBaseUri(httpContext.Request), GetFullUri(httpContext.Request));

        if (navigationManager != null)
        {
            navigationManager.NotFoundEvent += SetNotFoundResponse;
        }

        var authenticationStateProvider = httpContext.RequestServices.GetService<AuthenticationStateProvider>();
        if (authenticationStateProvider is IHostEnvironmentAuthenticationStateProvider hostEnvironmentAuthenticationStateProvider)
        {
            var authenticationState = new AuthenticationState(httpContext.User);
            hostEnvironmentAuthenticationStateProvider.SetAuthenticationState(Task.FromResult(authenticationState));
        }

        if (authenticationStateProvider != null)
        {
            var authStateListeners = httpContext.RequestServices.GetServices<IHostEnvironmentAuthenticationStateProvider>();
            Task<AuthenticationState>? authStateTask = null;
            foreach (var authStateListener in authStateListeners)
            {
                authStateTask ??= authenticationStateProvider.GetAuthenticationStateAsync();
                authStateListener.SetAuthenticationState(authStateTask);
            }
        }

        InitializeResourceCollection(httpContext);

        if (handler != null && form != null)
        {
            httpContext.RequestServices.GetRequiredService<HttpContextFormDataProvider>()
                .SetFormData(handler, new FormCollectionReadOnlyDictionary(form), form.Files);
        }