Skip to content

Commit d48e2bf

Browse files
committed
Remove circular DI, pass renderer on initialization instead.
1 parent e92baae commit d48e2bf

12 files changed

+37
-25
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Components;
5+
6+
/// <summary>
7+
/// An interface for EndpointHtmlRenderer implementations that allows NavigationManagers call renderer's methods
8+
/// </summary>
9+
public interface IEndpointHtmlRenderer
10+
{
11+
/// <summary>
12+
/// Sets the html response to 404 Not Found.
13+
/// </summary>
14+
void SetNotFoundResponse();
15+
}

src/Components/Components/src/PublicAPI.Shipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ Microsoft.AspNetCore.Components.RouteView.RouteData.set -> void
445445
Microsoft.AspNetCore.Components.RouteView.RouteView() -> void
446446
Microsoft.AspNetCore.Components.RouteView.SetParametersAsync(Microsoft.AspNetCore.Components.ParameterView parameters) -> System.Threading.Tasks.Task!
447447
Microsoft.AspNetCore.Components.Routing.IHostEnvironmentNavigationManager
448-
Microsoft.AspNetCore.Components.Routing.IHostEnvironmentNavigationManager.Initialize(string! baseUri, string! uri) -> void
449448
Microsoft.AspNetCore.Components.Routing.INavigationInterception
450449
Microsoft.AspNetCore.Components.Routing.INavigationInterception.EnableNavigationInterceptionAsync() -> System.Threading.Tasks.Task!
451450
Microsoft.AspNetCore.Components.Routing.IRoutingStateProvider

src/Components/Components/src/PublicAPI.Unshipped.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ Microsoft.AspNetCore.Components.NavigationManager.NotifyNotFound(bool isIntercep
1818
Microsoft.AspNetCore.Components.NavigationManager.NotifyNotFoundAsync(bool isNavigationIntercepted) -> System.Threading.Tasks.ValueTask<bool>
1919
virtual Microsoft.AspNetCore.Components.NavigationManager.HandleNotFoundHandlerException(System.Exception! ex, Microsoft.AspNetCore.Components.Routing.NotFoundContext! context) -> void
2020
Microsoft.AspNetCore.Components.NotFoundRenderingException
21-
Microsoft.AspNetCore.Components.NotFoundRenderingException.NotFoundRenderingException(string! message, System.Exception! innerException) -> void
21+
Microsoft.AspNetCore.Components.NotFoundRenderingException.NotFoundRenderingException(string! message, System.Exception! innerException) -> void
22+
Microsoft.AspNetCore.Components.IEndpointHtmlRenderer
23+
Microsoft.AspNetCore.Components.IEndpointHtmlRenderer.SetNotFoundResponse() -> void
24+
Microsoft.AspNetCore.Components.Routing.IHostEnvironmentNavigationManager.Initialize(string! baseUri, string! uri, Microsoft.AspNetCore.Components.IEndpointHtmlRenderer? renderer = null) -> void

src/Components/Components/src/Routing/IHostEnvironmentNavigationManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public interface IHostEnvironmentNavigationManager
1414
/// </summary>
1515
/// <param name="baseUri">The base URI.</param>
1616
/// <param name="uri">The absolute URI.</param>
17-
void Initialize(string baseUri, string uri);
17+
/// <param name="renderer">The optional renderer.</param>
18+
void Initialize(string baseUri, string uri, IEndpointHtmlRenderer? renderer = null);
1819
}

src/Components/Endpoints/src/DependencyInjection/HttpNavigationManager.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ namespace Microsoft.AspNetCore.Components.Endpoints;
77

88
internal sealed class HttpNavigationManager : NavigationManager, IHostEnvironmentNavigationManager
99
{
10-
private readonly IRazorComponentEndpointInvoker _invoker;
10+
private IEndpointHtmlRenderer? _renderer;
1111

12-
public HttpNavigationManager(IRazorComponentEndpointInvoker invoker)
12+
void IHostEnvironmentNavigationManager.Initialize(string baseUri, string uri, IEndpointHtmlRenderer? renderer)
1313
{
14-
_invoker = invoker;
14+
Initialize(baseUri, uri);
15+
_renderer = renderer;
1516
}
1617

17-
void IHostEnvironmentNavigationManager.Initialize(string baseUri, string uri) => Initialize(baseUri, uri);
18-
1918
protected override void NavigateToCore(string uri, NavigationOptions options)
2019
{
2120
var absoluteUriString = ToAbsoluteUri(uri).AbsoluteUri;
@@ -24,6 +23,6 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
2423

2524
protected override void NotFoundCore()
2625
{
27-
_invoker.SetNotFound();
26+
_renderer?.SetNotFoundResponse();
2827
}
2928
}

src/Components/Endpoints/src/IRazorComponentEndpointInvoker.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,4 @@ public interface IRazorComponentEndpointInvoker
2525
/// <returns>A <see cref="Task"/> that completes when the endpoint has been invoked and the component
2626
/// has been rendered into the response.</returns>
2727
Task Render(HttpContext context);
28-
29-
void SetNotFound();
3028
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
#nullable enable
2-
Microsoft.AspNetCore.Components.Endpoints.IRazorComponentEndpointInvoker.SetNotFound() -> void
1+
#nullable enable

src/Components/Endpoints/src/RazorComponentEndpointInvoker.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ public Task Render(HttpContext context)
3333
return _renderer.Dispatcher.InvokeAsync(() => RenderComponentCore(context));
3434
}
3535

36-
public void SetNotFound()
37-
{
38-
_renderer.SetNotFoundResponse();
39-
}
40-
4136
// We do not want the debugger to consider NavigationExceptions caught by this method as user-unhandled.
4237
[DebuggerDisableUserUnhandledExceptions]
4338
private async Task RenderComponentCore(HttpContext context)
@@ -79,7 +74,7 @@ private async Task RenderComponentCore(HttpContext context)
7974
return Task.CompletedTask;
8075
});
8176

82-
await EndpointHtmlRenderer.InitializeStandardComponentServicesAsync(
77+
await _renderer.InitializeStandardComponentServicesAsync(
8378
context,
8479
componentType: pageComponent,
8580
handler: result.HandlerName,

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.EventDispatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private Task ReturnErrorResponse(string detailedMessage)
7474
: Task.CompletedTask;
7575
}
7676

77-
internal void SetNotFoundResponse()
77+
public void SetNotFoundResponse()
7878
{
7979
_httpContext.Response.StatusCode = StatusCodes.Status404NotFound;
8080
}

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Components.Endpoints;
3535
/// output with prerendering markers so the content can later switch into interactive mode when used with
3636
/// blazor.*.js. It also deals with initializing the standard component DI services once per request.
3737
/// </summary>
38-
internal partial class EndpointHtmlRenderer : StaticHtmlRenderer, IComponentPrerenderer
38+
internal partial class EndpointHtmlRenderer : StaticHtmlRenderer, IComponentPrerenderer, IEndpointHtmlRenderer
3939
{
4040
private readonly IServiceProvider _services;
4141
private readonly RazorComponentsServiceOptions _options;
@@ -70,14 +70,14 @@ private void SetHttpContext(HttpContext httpContext)
7070
}
7171
}
7272

73-
internal static async Task InitializeStandardComponentServicesAsync(
73+
internal async Task InitializeStandardComponentServicesAsync(
7474
HttpContext httpContext,
7575
[DynamicallyAccessedMembers(Component)] Type? componentType = null,
7676
string? handler = null,
7777
IFormCollection? form = null)
7878
{
7979
var navigationManager = (IHostEnvironmentNavigationManager)httpContext.RequestServices.GetRequiredService<NavigationManager>();
80-
navigationManager?.Initialize(GetContextBaseUri(httpContext.Request), GetFullUri(httpContext.Request));
80+
navigationManager?.Initialize(GetContextBaseUri(httpContext.Request), GetFullUri(httpContext.Request), this);
8181

8282
var authenticationStateProvider = httpContext.RequestServices.GetService<AuthenticationStateProvider>();
8383
if (authenticationStateProvider is IHostEnvironmentAuthenticationStateProvider hostEnvironmentAuthenticationStateProvider)

0 commit comments

Comments
 (0)