Skip to content

Commit f91ac86

Browse files
committed
More API cleanups, make StaticAssetsDataSource internal
1 parent 19d4407 commit f91ac86

File tree

10 files changed

+88
-30
lines changed

10 files changed

+88
-30
lines changed

src/Components/Components.slnf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
"src\\Components\\WebAssembly\\Authentication.Msal\\src\\Microsoft.Authentication.WebAssembly.Msal.csproj",
2626
"src\\Components\\WebAssembly\\DevServer\\src\\Microsoft.AspNetCore.Components.WebAssembly.DevServer.csproj",
2727
"src\\Components\\WebAssembly\\JSInterop\\src\\Microsoft.JSInterop.WebAssembly.csproj",
28+
"src\\Components\\WebAssembly\\Samples\\HostedBlazorWebassemblyApp\\Client\\HostedBlazorWebassemblyApp.Client.csproj",
29+
"src\\Components\\WebAssembly\\Samples\\HostedBlazorWebassemblyApp\\Server\\HostedBlazorWebassemblyApp.Server.csproj",
30+
"src\\Components\\WebAssembly\\Samples\\HostedBlazorWebassemblyApp\\Shared\\HostedBlazorWebassemblyApp.Shared.csproj",
2831
"src\\Components\\WebAssembly\\Server\\src\\Microsoft.AspNetCore.Components.WebAssembly.Server.csproj",
2932
"src\\Components\\WebAssembly\\Server\\test\\Microsoft.AspNetCore.Components.WebAssembly.Server.Tests.csproj",
3033
"src\\Components\\WebAssembly\\WebAssembly.Authentication\\src\\Microsoft.AspNetCore.Components.WebAssembly.Authentication.csproj",

src/Components/WebAssembly/Samples/HostedBlazorWebassemblyApp/Server/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5656
{
5757
endpoints.MapRazorPages();
5858
endpoints.MapControllers();
59-
endpoints.MapStaticAssetEndpoints();
59+
endpoints.MapStaticAssets();
6060
endpoints.MapFallbackToPage("/_Host");
6161
});
6262
}

src/Components/WebAssembly/Server/src/Builder/WebAssemblyRazorComponentsEndpointConventionBuilderExtensions.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Linq;
45
using Microsoft.AspNetCore.Components.Endpoints;
56
using Microsoft.AspNetCore.Components.Endpoints.Infrastructure;
67
using Microsoft.AspNetCore.Components.Web;
78
using Microsoft.AspNetCore.Components.WebAssembly.Server;
89
using Microsoft.AspNetCore.Routing;
9-
using Microsoft.AspNetCore.StaticAssets;
10+
using Microsoft.AspNetCore.StaticAssets.Infrastructure;
1011
using Microsoft.Extensions.DependencyInjection;
1112
using Microsoft.Extensions.Hosting;
12-
using System.Linq;
13+
using Microsoft.Extensions.Logging;
1314

1415
namespace Microsoft.AspNetCore.Builder;
1516

1617
/// <summary>
1718
/// Web assembly specific endpoint conventions for razor component applications.
1819
/// </summary>
19-
public static class WebAssemblyRazorComponentsEndpointConventionBuilderExtensions
20+
public static partial class WebAssemblyRazorComponentsEndpointConventionBuilderExtensions
2021
{
2122
/// <summary>
2223
/// Configures the application to support the <see cref="RenderMode.InteractiveWebAssembly"/> render mode.
@@ -55,16 +56,26 @@ public static RazorComponentsEndpointConventionBuilder AddInteractiveWebAssembly
5556

5657
// If the static assets data source for the given manifest name is already added, then just wire-up the Blazor WebAssembly conventions.
5758
// MapStaticWebAssetEndpoints is idempotent and will not add the data source if it already exists.
58-
var staticAssetsManifestPath = options.StaticAssetsManifestPath ?? Path.Combine(AppContext.BaseDirectory, $"{environment.ApplicationName}.staticwebassets.endpoints.json");
59-
staticAssetsManifestPath = Path.IsPathRooted(staticAssetsManifestPath) ? staticAssetsManifestPath : Path.Combine(AppContext.BaseDirectory, staticAssetsManifestPath);
60-
if (HasStaticAssetDataSource(endpointBuilder, staticAssetsManifestPath))
59+
if (HasStaticAssetDataSource(endpointBuilder, options.StaticAssetsManifestPath))
6160
{
6261
options.ConventionsApplied = true;
63-
endpointBuilder.MapStaticAssets(staticAssetsManifestPath)
62+
endpointBuilder.MapStaticAssets(options.StaticAssetsManifestPath)
6463
.AddBlazorWebAssemblyConventions();
6564

6665
return builder;
6766
}
67+
else if (environment.IsDevelopment())
68+
{
69+
var logger = endpointBuilder.ServiceProvider.GetRequiredService<ILogger<WebAssemblyComponentsEndpointOptions>>();
70+
if (options.StaticAssetsManifestPath is null)
71+
{
72+
Log.StaticAssetsMappingNotFoundForDefaultManifest(logger);
73+
}
74+
else
75+
{
76+
Log.StaticAssetsMappingNotFoundWithManifest(logger, options.StaticAssetsManifestPath);
77+
}
78+
}
6879

6980
return builder;
7081
}
@@ -73,13 +84,21 @@ private static bool HasStaticAssetDataSource(IEndpointRouteBuilder endpointRoute
7384
{
7485
foreach (var ds in endpointRouteBuilder.DataSources)
7586
{
76-
if (ds is StaticAssetsEndpointDataSource staticAssetsDataSource &&
77-
string.Equals(Path.GetFileName(staticAssetsDataSource.ManifestName), staticAssetsManifestName, StringComparison.OrdinalIgnoreCase))
87+
if (StaticAssetsEndpointDataSourceHelper.IsStaticAssetsDataSource(ds, staticAssetsManifestName))
7888
{
7989
return true;
8090
}
8191
}
8292

8393
return false;
8494
}
95+
96+
internal static partial class Log
97+
{
98+
[LoggerMessage(1, LogLevel.Warning, $$"""Mapped static asset endpoints not found. Ensure '{{nameof(StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets)}}' is called before '{{nameof(AddInteractiveWebAssemblyRenderMode)}}'.""")]
99+
internal static partial void StaticAssetsMappingNotFoundForDefaultManifest(ILogger logger);
100+
101+
[LoggerMessage(2, LogLevel.Warning, $$"""Mapped static asset endpoints not found for manifest '{ManifestPath}'. Ensure '{{nameof(StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets)}}'(staticAssetsManifestPath) is called before '{{nameof(AddInteractiveWebAssemblyRenderMode)}}' and that both manifest paths are the same.""")]
102+
internal static partial void StaticAssetsMappingNotFoundWithManifest(ILogger logger, string manifestPath);
103+
}
85104
}

src/Http/Routing/src/PublicAPI.Unshipped.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,4 @@ Microsoft.AspNetCore.Routing.ContentEncodingMetadata
33
Microsoft.AspNetCore.Routing.ContentEncodingMetadata.ContentEncodingMetadata(string! value, double quality) -> void
44
Microsoft.AspNetCore.Routing.ContentEncodingMetadata.Quality.get -> double
55
Microsoft.AspNetCore.Routing.ContentEncodingMetadata.Value.get -> string!
6-
Microsoft.AspNetCore.Routing.Matching.ContentEncodingMetadata
7-
Microsoft.AspNetCore.Routing.Matching.ContentEncodingMetadata.ContentEncodingMetadata(string! value, double quality) -> void
8-
Microsoft.AspNetCore.Routing.Matching.ContentEncodingMetadata.Quality.get -> double
9-
Microsoft.AspNetCore.Routing.Matching.ContentEncodingMetadata.Value.get -> string!
106
static Microsoft.AspNetCore.Routing.RouteHandlerServices.Map(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, string! pattern, System.Delegate! handler, System.Collections.Generic.IEnumerable<string!>? httpMethods, System.Func<System.Reflection.MethodInfo!, Microsoft.AspNetCore.Http.RequestDelegateFactoryOptions?, Microsoft.AspNetCore.Http.RequestDelegateMetadataResult!>! populateMetadata, System.Func<System.Delegate!, Microsoft.AspNetCore.Http.RequestDelegateFactoryOptions!, Microsoft.AspNetCore.Http.RequestDelegateMetadataResult?, Microsoft.AspNetCore.Http.RequestDelegateResult!>! createRequestDelegate, System.Reflection.MethodInfo! methodInfo) -> Microsoft.AspNetCore.Builder.RouteHandlerBuilder!
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.AspNetCore.Routing;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace Microsoft.AspNetCore.StaticAssets.Infrastructure;
9+
10+
/// <summary>
11+
/// This type is not recommended for use outside of ASP.NET Core.
12+
/// </summary>
13+
public static class StaticAssetsEndpointDataSourceHelper
14+
{
15+
/// <summary>
16+
/// This method is not recommended for use outside of ASP.NET Core.
17+
/// </summary>
18+
/// <param name="dataSource"></param>
19+
/// <param name="staticAssetsManifestPath"></param>
20+
21+
public static bool IsStaticAssetsDataSource(EndpointDataSource dataSource, string? staticAssetsManifestPath = null)
22+
{
23+
if (dataSource is StaticAssetsEndpointDataSource staticAssetsDataSource)
24+
{
25+
if (staticAssetsManifestPath is null)
26+
{
27+
var serviceProvider = staticAssetsDataSource.ServiceProvider;
28+
var environment = serviceProvider.GetRequiredService<IWebHostEnvironment>();
29+
staticAssetsManifestPath = Path.Combine(AppContext.BaseDirectory, $"{environment.ApplicationName}.staticwebassets.endpoints.json");
30+
}
31+
32+
staticAssetsManifestPath = Path.IsPathRooted(staticAssetsManifestPath) ? staticAssetsManifestPath : Path.Combine(AppContext.BaseDirectory, staticAssetsManifestPath);
33+
34+
return string.Equals(staticAssetsDataSource.ManifestPath, staticAssetsManifestPath, StringComparison.Ordinal);
35+
}
36+
37+
return false;
38+
}
39+
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions
1+
Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions
2+
Microsoft.AspNetCore.StaticAssets.Infrastructure.StaticAssetsEndpointDataSourceHelper
23
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder
34
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder.Add(System.Action<Microsoft.AspNetCore.Builder.EndpointBuilder!>! convention) -> void
45
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder.Finally(System.Action<Microsoft.AspNetCore.Builder.EndpointBuilder!>! convention) -> void
5-
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointDataSource
6-
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointDataSource.DefaultBuilder.get -> Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder!
7-
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointDataSource.ManifestName.get -> string!
8-
override Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointDataSource.Endpoints.get -> System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Http.Endpoint!>!
9-
override Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointDataSource.GetChangeToken() -> Microsoft.Extensions.Primitives.IChangeToken!
10-
static Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, string? staticAssetsManifestPath = null) -> Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder!
6+
static Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, string? staticAssetsManifestPath = null) -> Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder!
7+
static Microsoft.AspNetCore.StaticAssets.Infrastructure.StaticAssetsEndpointDataSourceHelper.IsStaticAssetsDataSource(Microsoft.AspNetCore.Routing.EndpointDataSource! dataSource, string? staticAssetsManifestPath = null) -> bool

src/StaticAssets/src/StaticAssetEndpointDataSource.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ namespace Microsoft.AspNetCore.StaticAssets;
1313
/// An <see cref="EndpointDataSource"/> for static assets.
1414
/// </summary>
1515
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
16-
public class StaticAssetsEndpointDataSource : EndpointDataSource
16+
internal class StaticAssetsEndpointDataSource : EndpointDataSource
1717
{
1818
private readonly object _lock = new();
1919
private readonly StaticAssetsManifest _manifest;
20-
private readonly string _manifestName;
2120
private readonly StaticAssetEndpointFactory _endpointFactory;
2221
private readonly List<Action<EndpointBuilder>> _conventions = [];
2322
private readonly List<Action<EndpointBuilder>> _finallyConventions = [];
2423
private List<Endpoint> _endpoints = null!;
2524
private CancellationTokenSource _cancellationTokenSource;
2625
private CancellationChangeToken _changeToken;
2726

28-
internal StaticAssetsEndpointDataSource(StaticAssetsManifest manifest, StaticAssetEndpointFactory endpointFactory, string manifestName, List<StaticAssetDescriptor> descriptors)
27+
internal StaticAssetsEndpointDataSource(IServiceProvider serviceProvider, StaticAssetsManifest manifest, StaticAssetEndpointFactory endpointFactory, string manifestName, List<StaticAssetDescriptor> descriptors)
2928
{
29+
ServiceProvider = serviceProvider;
3030
_manifest = manifest;
31-
_manifestName = manifestName;
31+
ManifestPath = manifestName;
3232
_endpointFactory = endpointFactory;
3333
_cancellationTokenSource = new CancellationTokenSource();
3434
_changeToken = new CancellationChangeToken(_cancellationTokenSource.Token);
@@ -43,7 +43,7 @@ internal StaticAssetsEndpointDataSource(StaticAssetsManifest manifest, StaticAss
4343
/// <summary>
4444
/// Gets the manifest name associated with this static asset endpoint data source.
4545
/// </summary>
46-
public string ManifestName => _manifestName;
46+
public string ManifestPath { get; }
4747

4848
/// <inheritdoc />
4949
internal StaticAssetsEndpointConventionBuilder DefaultBuilder { get; set; }
@@ -67,6 +67,8 @@ public override IReadOnlyList<Endpoint> Endpoints
6767
}
6868
}
6969

70+
internal IServiceProvider ServiceProvider { get; }
71+
7072
private void Initialize()
7173
{
7274
if (_endpoints == null)
@@ -110,5 +112,8 @@ public override IChangeToken GetChangeToken()
110112
return _changeToken;
111113
}
112114

113-
private string GetDebuggerDisplay() => _manifestName;
115+
private string GetDebuggerDisplay()
116+
{
117+
return ManifestPath;
118+
}
114119
}

src/StaticAssets/src/StaticAssetEndpointFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.AspNetCore.Hosting;
77
using Microsoft.AspNetCore.Http;
88
using Microsoft.AspNetCore.Routing;
9-
using Microsoft.AspNetCore.Routing.Matching;
109
using Microsoft.AspNetCore.Routing.Patterns;
1110
using Microsoft.Extensions.DependencyInjection;
1211
using Microsoft.Extensions.Logging;

src/StaticAssets/src/StaticAssetsEndpointRouteBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static StaticAssetsEndpointConventionBuilder MapStaticAssetEndpointsCore
5151
{
5252
foreach (var ds in endpoints.DataSources)
5353
{
54-
if (ds is StaticAssetsEndpointDataSource sads && sads.ManifestName.Equals(manifestPath, StringComparison.Ordinal))
54+
if (ds is StaticAssetsEndpointDataSource sads && sads.ManifestPath.Equals(manifestPath, StringComparison.Ordinal))
5555
{
5656
return sads.DefaultBuilder;
5757
}

src/StaticAssets/src/StaticAssetsManifest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal static StaticAssetsManifest Parse(string manifestPath)
3737

3838
internal StaticAssetsEndpointDataSource CreateDataSource(IEndpointRouteBuilder endpoints, string manifestName, List<StaticAssetDescriptor> descriptors)
3939
{
40-
var dataSource = new StaticAssetsEndpointDataSource(this, new StaticAssetEndpointFactory(endpoints.ServiceProvider), manifestName, descriptors);
40+
var dataSource = new StaticAssetsEndpointDataSource(endpoints.ServiceProvider, this, new StaticAssetEndpointFactory(endpoints.ServiceProvider), manifestName, descriptors);
4141
endpoints.DataSources.Add(dataSource);
4242
return dataSource;
4343
}

0 commit comments

Comments
 (0)