Skip to content

Commit ee38935

Browse files
committed
Replace hosted service with init extension
1 parent 807adf6 commit ee38935

9 files changed

Lines changed: 207 additions & 216 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
## Unreleased
44

5-
- Initial standalone release preparation.
6-
- Prepared the package for independent build, test, and NuGet packaging.
5+
- Replaced hosted-service startup with explicit `InitializeHashRoutingAsync()` host initialization.
6+
- Canonicalized internal anchor `href` values to hash-route URLs for browser-managed navigation.
7+
- Clarified that relative anchor URLs such as `./foo` resolve relative to the application base URI.
78

89
## 1.0.0-rc.1
910

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ Register hash routing during startup:
1616

1717
```csharp
1818
using Blazor.HashRouting;
19+
using Microsoft.Extensions.Hosting;
1920

2021
var builder = WebAssemblyHostBuilder.CreateDefault(args);
2122

2223
builder.Services.AddHashRouting();
2324

24-
await builder.Build().RunAsync();
25+
var host = builder.Build();
26+
await host.InitializeHashRoutingAsync();
27+
await host.RunAsync();
2528
```
2629

30+
Call `InitializeHashRoutingAsync()` after `Build()` and before `RunAsync()`.
31+
2732
Optional configuration:
2833

2934
```csharp

release-notes/1.1.0-rc.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ Release candidate build for the `{{MAJOR_MINOR_PATCH}}` line of `Blazor.HashRout
66

77
This release improves link behavior for hash-routed applications by canonicalizing internal anchor `href` values to `/#/...` URLs so browser-managed actions such as middle-click, right-click open in new tab, and copied links continue to resolve through the hash router.
88

9+
This release also switches startup initialization to an explicit host extension. Applications should call `InitializeHashRoutingAsync()` after building the host and before running it.
10+
911
## Highlights
1012

1113
- Canonicalizes internal anchor `href` values to hash-route URLs
1214
- Preserves opt-out behavior for links that use non-`_self` targets or `download`
1315
- Keeps browser-managed navigation aligned with intercepted left-click navigation
1416
- Continues to support base-path deployments such as `/proxy/app/#/settings`
17+
- Replaces hosted-service startup with explicit `InitializeHashRoutingAsync()` host initialization
1518

1619
## Relative link resolution
1720

release-notes/1.1.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ Stable release for the `{{MAJOR_MINOR_PATCH}}` line of `Blazor.HashRouting`.
66

77
This release improves link behavior for hash-routed applications by canonicalizing internal anchor `href` values to `/#/...` URLs so browser-managed actions such as middle-click, right-click open in new tab, and copied links continue to resolve through the hash router.
88

9+
This release also switches startup initialization to an explicit host extension. Applications should call `InitializeHashRoutingAsync()` after building the host and before running it.
10+
911
## Highlights
1012

1113
- Canonicalizes internal anchor `href` values to hash-route URLs
1214
- Preserves opt-out behavior for links that use non-`_self` targets or `download`
1315
- Keeps browser-managed navigation aligned with intercepted left-click navigation
1416
- Continues to support base-path deployments such as `/proxy/app/#/settings`
17+
- Replaces hosted-service startup with explicit `InitializeHashRoutingAsync()` host initialization
1518

1619
## Relative link resolution
1720

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Blazor.HashRouting;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
5+
namespace Microsoft.Extensions.Hosting
6+
{
7+
/// <summary>
8+
/// Host initialization extensions for hash routing.
9+
/// </summary>
10+
public static class HashRoutingHostExtensions
11+
{
12+
/// <summary>
13+
/// Initializes hash routing after the host has been built.
14+
/// </summary>
15+
/// <param name="host">The application host.</param>
16+
/// <param name="cancellationToken">A cancellation token.</param>
17+
/// <returns>A task representing the asynchronous initialization operation.</returns>
18+
public static Task InitializeHashRoutingAsync(this IHost host, CancellationToken cancellationToken = default)
19+
{
20+
ArgumentNullException.ThrowIfNull(host);
21+
22+
if (!OperatingSystem.IsBrowser())
23+
{
24+
throw new PlatformNotSupportedException("Blazor.HashRouting supports browser-hosted Blazor WebAssembly applications only.");
25+
}
26+
27+
return InitializeHashRoutingCoreAsync(host, cancellationToken);
28+
}
29+
30+
internal static Task InitializeHashRoutingCoreAsync(this IHost host, CancellationToken cancellationToken = default)
31+
{
32+
ArgumentNullException.ThrowIfNull(host);
33+
34+
var navigationManager = host.Services.GetRequiredService<HashNavigationManager>();
35+
36+
return navigationManager.InitializeAsync(cancellationToken);
37+
}
38+
}
39+
}

src/Blazor.HashRouting/HashRoutingHostedService.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Blazor.HashRouting/ServiceCollectionExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.AspNetCore.Components;
22
using Microsoft.Extensions.DependencyInjection.Extensions;
3-
using Microsoft.Extensions.Hosting;
43
using Blazor.HashRouting;
54
using Microsoft.Extensions.Logging;
65
using Microsoft.JSInterop;
@@ -50,7 +49,6 @@ internal static IServiceCollection AddHashRoutingCore(this IServiceCollection se
5049
// Keep AddHashRouting idempotent while allowing the latest options registration to win.
5150
services.RemoveAll<HashRoutingOptions>();
5251
services.AddSingleton(options);
53-
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, HashRoutingHostedService>());
5452
return services;
5553
}
5654

@@ -86,7 +84,6 @@ internal static IServiceCollection AddHashRoutingCore(this IServiceCollection se
8684
return (HashNavigationManager)serviceProvider.GetRequiredService<NavigationManager>();
8785
});
8886
services.AddSingleton<HashRoutingRegistrationMarker>();
89-
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, HashRoutingHostedService>());
9087

9188
return services;
9289
}

0 commit comments

Comments
 (0)