Przeglądaj źródła

[Blazor] Invoke inbound activity handlers on circuit initialization (#57557)

Mackinnon Buck 1 rok temu
rodzic
commit
e2db47cfcd

+ 2 - 2
src/Components/Server/src/Circuits/CircuitHost.cs

@@ -104,7 +104,7 @@ internal partial class CircuitHost : IAsyncDisposable
     {
         Log.InitializationStarted(_logger);
 
-        return Renderer.Dispatcher.InvokeAsync(async () =>
+        return HandleInboundActivityAsync(() => Renderer.Dispatcher.InvokeAsync(async () =>
         {
             if (_initialized)
             {
@@ -165,7 +165,7 @@ internal partial class CircuitHost : IAsyncDisposable
                 UnhandledException?.Invoke(this, new UnhandledExceptionEventArgs(ex, isTerminating: false));
                 await TryNotifyClientErrorAsync(Client, GetClientErrorMessage(ex), ex);
             }
-        });
+        }));
     }
 
     // We handle errors in DisposeAsync because there's no real value in letting it propagate.

+ 22 - 11
src/Components/test/E2ETest/ServerExecutionTests/CircuitContextTest.cs

@@ -22,28 +22,39 @@ public class CircuitContextTest : ServerTestBase<BasicTestAppServerSiteFixture<S
     {
     }
 
-    protected override void InitializeAsyncCore()
+    [Fact]
+    public void ComponentMethods_HaveCircuitContext()
     {
         Navigate(ServerPathBase);
         Browser.MountTestComponent<CircuitContextComponent>();
-        Browser.Equal("Circuit Context", () => Browser.Exists(By.TagName("h1")).Text);
+        TestCircuitContextCore(Browser);
     }
 
     [Fact]
-    public void ComponentMethods_HaveCircuitContext()
+    public void ComponentMethods_HaveCircuitContext_OnInitialPageLoad()
     {
-        Browser.Click(By.Id("trigger-click-event-button"));
+        // https://github.com/dotnet/aspnetcore/issues/57481
+        Navigate($"{ServerPathBase}?initial-component-type={typeof(CircuitContextComponent).AssemblyQualifiedName}");
+        TestCircuitContextCore(Browser);
+    }
+
+    // Internal for reuse in Blazor Web tests
+    internal static void TestCircuitContextCore(IWebDriver browser)
+    {
+        browser.Equal("Circuit Context", () => browser.Exists(By.TagName("h1")).Text);
+
+        browser.Click(By.Id("trigger-click-event-button"));
 
-        Browser.True(() => HasCircuitContext("SetParametersAsync"));
-        Browser.True(() => HasCircuitContext("OnInitializedAsync"));
-        Browser.True(() => HasCircuitContext("OnParametersSetAsync"));
-        Browser.True(() => HasCircuitContext("OnAfterRenderAsync"));
-        Browser.True(() => HasCircuitContext("InvokeDotNet"));
-        Browser.True(() => HasCircuitContext("OnClickEvent"));
+        browser.True(() => HasCircuitContext("SetParametersAsync"));
+        browser.True(() => HasCircuitContext("OnInitializedAsync"));
+        browser.True(() => HasCircuitContext("OnParametersSetAsync"));
+        browser.True(() => HasCircuitContext("OnAfterRenderAsync"));
+        browser.True(() => HasCircuitContext("InvokeDotNet"));
+        browser.True(() => HasCircuitContext("OnClickEvent"));
 
         bool HasCircuitContext(string eventName)
         {
-            var resultText = Browser.FindElement(By.Id($"circuit-context-result-{eventName}")).Text;
+            var resultText = browser.FindElement(By.Id($"circuit-context-result-{eventName}")).Text;
             var result = bool.Parse(resultText);
             return result;
         }

+ 2 - 2
src/Components/test/E2ETest/ServerRenderingTests/InteractivityTest.cs

@@ -4,6 +4,7 @@
 using Components.TestServer.RazorComponents;
 using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
 using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
+using Microsoft.AspNetCore.Components.E2ETests.ServerExecutionTests;
 using Microsoft.AspNetCore.E2ETesting;
 using OpenQA.Selenium;
 using TestServer;
@@ -1142,8 +1143,7 @@ public class InteractivityTest : ServerTestBase<BasicTestAppServerSiteFixture<Ra
     public void InteractiveServerRootComponent_CanAccessCircuitContext()
     {
         Navigate($"{ServerPathBase}/interactivity/circuit-context");
-
-        Browser.Equal("True", () => Browser.FindElement(By.Id("has-circuit-context")).Text);
+        CircuitContextTest.TestCircuitContextCore(Browser);
     }
 
     [Fact]

+ 11 - 0
src/Components/test/testassets/BasicTestApp/Index.razor

@@ -1,4 +1,6 @@
 @using Microsoft.AspNetCore.Components.Rendering
+@using System.Web
+@inject NavigationManager NavigationManager
 <div id="test-selector">
     Select test:
     <select id="test-selector-select" @bind=SelectedComponentTypeName>
@@ -137,6 +139,15 @@
     Type SelectedComponentType
         => SelectedComponentTypeName == "none" ? null : Type.GetType(SelectedComponentTypeName, throwOnError: true);
 
+    protected override void OnInitialized()
+    {
+        var uri = new Uri(NavigationManager.Uri);
+        if (HttpUtility.ParseQueryString(uri.Query)["initial-component-type"] is { Length: > 0 } initialComponentTypeName)
+        {
+            SelectedComponentTypeName = initialComponentTypeName;
+        }
+    }
+
     void RenderSelectedComponent(RenderTreeBuilder builder)
     {
         if (SelectedComponentType != null)

+ 1 - 0
src/Components/test/testassets/Components.TestServer/RazorComponents/App.razor

@@ -25,6 +25,7 @@
     </script>
     <script src="_framework/blazor.web.js" autostart="false" suppress-error="BL9992"></script>
     <script src="_content/TestContentPackage/counterInterop.js"></script>
+    <script src="js/circuitContextTest.js"></script>
     <script>
         // This is called by the Components.WasmMinimal project.
         function getQueryParam(key) {

+ 1 - 23
src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Interactivity/CircuitContextPage.razor

@@ -1,25 +1,3 @@
 @page "/interactivity/circuit-context"
-@rendermode RenderMode.InteractiveServer
-@inject TestCircuitContextAccessor CircuitContextAccessor
 
-<h1>Circuit context</h1>
-
-<p>
-    Has circuit context: <span id="has-circuit-context">@_hasCircuitContext</span>
-</p>
-
-@code {
-    private bool _hasCircuitContext;
-
-    protected override async Task OnAfterRenderAsync(bool firstRender)
-    {
-        if (firstRender)
-        {
-            await Task.Yield();
-
-            _hasCircuitContext = CircuitContextAccessor.HasCircuitContext;
-
-            StateHasChanged();
-        }
-    }
-}
+<CircuitContextComponent @rendermode="RenderMode.InteractiveServer" />