-
Notifications
You must be signed in to change notification settings - Fork 10.3k
[Blazor] Add IRazorComponentsBuilder.AddWebAssemblyComponents()
#48664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e9141cc
Add 'AddWebAssemblyComponents()'
MackinnonBuck 94b5069
PR feedback + new APIs + basic E2E tests
MackinnonBuck e2bf79c
Update Microsoft.AspNetCore.Components.WebAssembly.Server.csproj
MackinnonBuck 04ed250
Keep shipping WebAssembly.Server as a package
MackinnonBuck 49842f5
Fix tests
MackinnonBuck 71cd4f7
Update Framework.slnf
MackinnonBuck 63dab0c
Create Directory.Build.props
MackinnonBuck 133f167
Move AddWebAssemblyComponents back to WebAssembly.Server
MackinnonBuck 688b30a
Update SharedFramework.Local.props
MackinnonBuck a944a22
Update PublicAPI.Unshipped.txt
MackinnonBuck d8fa166
Use Add{Server|WebAssembly}RenderMode()
MackinnonBuck 0f56f1a
Update src/Components/Endpoints/src/DependencyInjection/WebAssemblyCo…
MackinnonBuck 52f364f
Rename FrameworkFilesPathPrefix to PathPrefix
MackinnonBuck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Components/Endpoints/src/DependencyInjection/WebAssemblyComponentsEndpointOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints; | ||
|
||
/// <summary> | ||
/// Options to configure interactive WebAssembly components. | ||
/// </summary> | ||
public sealed class WebAssemblyComponentsEndpointOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets the <see cref="PathString"/> that indicates the prefix for Blazor WebAssembly assets. | ||
/// This path must correspond to a referenced Blazor WebAssembly application project. | ||
/// </summary> | ||
public PathString PathPrefix { get; set; } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Components/WebAssembly/Server/src/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Components.WebAssembly.Server.TargetPickerUi.DisplayFirefox(Microsoft.AspNetCore.Http.HttpContext! context) -> System.Threading.Tasks.Task! | ||
Microsoft.Extensions.DependencyInjection.RazorComponentsBuilderExtensions | ||
static Microsoft.Extensions.DependencyInjection.RazorComponentsBuilderExtensions.AddWebAssemblyComponents(this Microsoft.AspNetCore.Components.Endpoints.IRazorComponentsBuilder! builder, System.Action<Microsoft.AspNetCore.Components.Endpoints.WebAssemblyComponentsEndpointOptions!>? configure = null) -> Microsoft.AspNetCore.Components.Endpoints.IRazorComponentsBuilder! |
112 changes: 112 additions & 0 deletions
112
src/Components/WebAssembly/Server/src/RazorComponentsBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Endpoints; | ||
using Microsoft.AspNetCore.Components.Web; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extension methods to configure an <see cref="IServiceCollection"/> for WebAssembly components. | ||
/// </summary> | ||
public static class RazorComponentsBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds services to support rendering interactive WebAssembly components. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IRazorComponentsBuilder"/>.</param> | ||
/// <param name="configure">A callback to configure <see cref="WebAssemblyComponentsEndpointOptions"/>.</param> | ||
/// <returns>An <see cref="IRazorComponentsBuilder"/> that can be used to further customize the configuration.</returns> | ||
public static IRazorComponentsBuilder AddWebAssemblyComponents(this IRazorComponentsBuilder builder, Action<WebAssemblyComponentsEndpointOptions>? configure = null) | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder, nameof(builder)); | ||
|
||
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<RenderModeEndpointProvider, WebAssemblyEndpointProvider>()); | ||
|
||
if (configure is not null) | ||
{ | ||
builder.Services.Configure(configure); | ||
} | ||
|
||
return builder; | ||
} | ||
|
||
private class WebAssemblyEndpointProvider : RenderModeEndpointProvider | ||
MackinnonBuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private readonly IServiceProvider _services; | ||
private readonly WebAssemblyComponentsEndpointOptions _options; | ||
|
||
public WebAssemblyEndpointProvider(IServiceProvider services, IOptions<WebAssemblyComponentsEndpointOptions> options) | ||
{ | ||
_services = services; | ||
_options = options.Value; | ||
} | ||
|
||
public override IEnumerable<RouteEndpointBuilder> GetEndpointBuilders(IComponentRenderMode renderMode, IApplicationBuilder applicationBuilder) | ||
{ | ||
var endpointRouteBuilder = new EndpointRouteBuilder(_services, applicationBuilder); | ||
var pathPrefix = _options.PathPrefix; | ||
|
||
applicationBuilder.UseBlazorFrameworkFiles(pathPrefix); | ||
var app = applicationBuilder.Build(); | ||
|
||
endpointRouteBuilder.Map($"{pathPrefix}/_framework/{{*path}}", context => | ||
{ | ||
// Set endpoint to null so the static files middleware will handle the request. | ||
context.SetEndpoint(null); | ||
|
||
return app(context); | ||
}); | ||
|
||
return endpointRouteBuilder.GetEndpoints(); | ||
} | ||
|
||
public override bool Supports(IComponentRenderMode renderMode) | ||
=> renderMode is WebAssemblyRenderMode or AutoRenderMode; | ||
|
||
private class EndpointRouteBuilder : IEndpointRouteBuilder | ||
{ | ||
private readonly IApplicationBuilder _applicationBuilder; | ||
|
||
public EndpointRouteBuilder(IServiceProvider serviceProvider, IApplicationBuilder applicationBuilder) | ||
{ | ||
ServiceProvider = serviceProvider; | ||
_applicationBuilder = applicationBuilder; | ||
} | ||
|
||
public IServiceProvider ServiceProvider { get; } | ||
|
||
public ICollection<EndpointDataSource> DataSources { get; } = new List<EndpointDataSource>() { }; | ||
|
||
public IApplicationBuilder CreateApplicationBuilder() | ||
{ | ||
return _applicationBuilder.New(); | ||
} | ||
|
||
internal IEnumerable<RouteEndpointBuilder> GetEndpoints() | ||
{ | ||
foreach (var ds in DataSources) | ||
{ | ||
foreach (var endpoint in ds.Endpoints) | ||
{ | ||
var routeEndpoint = (RouteEndpoint)endpoint; | ||
var builder = new RouteEndpointBuilder(endpoint.RequestDelegate, routeEndpoint.RoutePattern, routeEndpoint.Order); | ||
for (var i = 0; i < routeEndpoint.Metadata.Count; i++) | ||
{ | ||
var metadata = routeEndpoint.Metadata[i]; | ||
builder.Metadata.Add(metadata); | ||
} | ||
|
||
yield return builder; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/Components/test/E2ETest/ServerRenderingTests/InteractivityTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Components.TestServer.RazorComponents; | ||
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure; | ||
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; | ||
using Microsoft.AspNetCore.E2ETesting; | ||
using OpenQA.Selenium; | ||
using TestServer; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests; | ||
|
||
public class InteractivityTest : ServerTestBase<BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>>> | ||
{ | ||
public InteractivityTest( | ||
BrowserFixture browserFixture, | ||
BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>> serverFixture, | ||
ITestOutputHelper output) | ||
: base(browserFixture, serverFixture, output) | ||
{ | ||
} | ||
|
||
public override Task InitializeAsync() | ||
=> InitializeAsync(BrowserFixture.StreamingContext); | ||
|
||
[Fact] | ||
public void CanRenderInteractiveServerComponent() | ||
{ | ||
// '2' configures the increment amount. | ||
Navigate($"{ServerPathBase}/interactive?server=2"); | ||
|
||
Browser.Equal("0", () => Browser.FindElement(By.Id("count-server")).Text); | ||
Browser.Equal("True", () => Browser.FindElement(By.Id("is-interactive-server")).Text); | ||
|
||
Browser.Click(By.Id("increment-server")); | ||
|
||
Browser.Equal("2", () => Browser.FindElement(By.Id("count-server")).Text); | ||
} | ||
|
||
[Fact] | ||
public void CanRenderInteractiveServerComponentFromRazorClassLibrary() | ||
{ | ||
// '3' configures the increment amount. | ||
Navigate($"{ServerPathBase}/interactive?server-shared=3"); | ||
|
||
Browser.Equal("0", () => Browser.FindElement(By.Id("count-server-shared")).Text); | ||
Browser.Equal("True", () => Browser.FindElement(By.Id("is-interactive-server-shared")).Text); | ||
|
||
Browser.Click(By.Id("increment-server-shared")); | ||
|
||
Browser.Equal("3", () => Browser.FindElement(By.Id("count-server-shared")).Text); | ||
} | ||
|
||
[Fact] | ||
public void CanRenderInteractiveWebAssemblyComponentFromRazorClassLibrary() | ||
{ | ||
// '4' configures the increment amount. | ||
Navigate($"{ServerPathBase}/interactive?wasm-shared=4"); | ||
|
||
Browser.Equal("0", () => Browser.FindElement(By.Id("count-wasm-shared")).Text); | ||
Browser.Equal("True", () => Browser.FindElement(By.Id("is-interactive-wasm-shared")).Text); | ||
|
||
Browser.Click(By.Id("increment-wasm-shared")); | ||
|
||
Browser.Equal("4", () => Browser.FindElement(By.Id("count-wasm-shared")).Text); | ||
} | ||
|
||
[Fact] | ||
public void CanRenderInteractiveServerAndWebAssemblyComponentsAtTheSameTime() | ||
{ | ||
// '3' and '5' configure the increment amounts. | ||
Navigate($"{ServerPathBase}/interactive?server-shared=3&wasm-shared=5"); | ||
|
||
Browser.Equal("0", () => Browser.FindElement(By.Id("count-server-shared")).Text); | ||
Browser.Equal("0", () => Browser.FindElement(By.Id("count-wasm-shared")).Text); | ||
Browser.Equal("True", () => Browser.FindElement(By.Id("is-interactive-server-shared")).Text); | ||
Browser.Equal("True", () => Browser.FindElement(By.Id("is-interactive-wasm-shared")).Text); | ||
|
||
Browser.Click(By.Id("increment-server-shared")); | ||
Browser.Click(By.Id("increment-wasm-shared")); | ||
|
||
Browser.Equal("3", () => Browser.FindElement(By.Id("count-server-shared")).Text); | ||
Browser.Equal("5", () => Browser.FindElement(By.Id("count-wasm-shared")).Text); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...estassets/Components.TestServer/RazorComponents/Components/ServerInteractiveCounter.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@attribute [RenderModeServer] | ||
|
||
<strong>Server counter</strong> | ||
<TestContentPackage.Counter IncrementAmount="IncrementAmount" IdSuffix="server" /> | ||
|
||
@code { | ||
[Parameter] | ||
public int IncrementAmount { get; set; } = 1; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.