Similar to this question from two years ago, I have a Blazor server-side app and the OnAfterRender() method
isn't firing. If I set a breakpoint at the start of the method, the breakpoint isn't hit.
I created a minimal example page in my project:
@page "/index2"
<h3>Index2</h3>
<p>Message: @msg</p>
@code {
private string msg = "Hello";
private bool flag = false;
protected override void OnAfterRender(bool firstRender)
{
if (!flag)
{
msg = " world";
flag = true;
StateHasChanged();
}
}
}
When I load this page, it displays "Message: Hello", without the "world" part ever appearing.
This is a project I haven't touched since in three months, and it worked perfectly a month ago. The only change I can think of is that I upgrading my OS to Windows 11. I have not tried running my project on Windows 10 yet; that may be my next attempt, but I'm hoping someone here has a more logical explanation. (Without a convincing reason why my OS would have an impact, that feels a bit like "magical thinking").
My _Host.cshtml has this as the final line in its body: <script src="_framework/blazor.server.js"></script>
My Startup.cs file has this as its last command in its Configure()
method:
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
My project targets .NET 6.0.
The frustrating thing is that I've spent 19 months on this project and it all worked beautifully. I upgraded from Visual Studio 2019 to Visual Studio 2022 and it still worked. It just suddenly stopped working after I upgraded my operating system to Windows 11.
What I've tried:
I tried the solutions suggested for this question and verified that my _Host.cshtml and Startup.cs had the code suggested there (which both already did).
I tried uninstalling Visual Studio 2022 and reverting to Visual Studio 2019. My project no longer loaded in Visual Studio 2019 because my installed version of MSBuild had changed, and I remembered that I had previously had the project working in Visual Studio 2022, so reverting there was probably a dead end anyway.
I tried creating a completely fresh Blazor project, and in that fresh project the OnAfterRender()
method did work as expected! But I'm completely flummoxed as to what could have broken in this preexisting project.
CodePudding user response:
Take your code, paste it into index
in a vanilla Blazor Server or WASM deployed project and it works. The OS version will make no difference. [Polite] There are things that you are telling us!
On OnAfterRender{async}
usage, it should (in general) only be used to do JS interop stuff. The component has already rendered, why do something where you need to force it to render again! Anything that manipulates the component instance data and needs to call StateHasChanged
to make it appear should be coded into the normal lifecycle events.
CodePudding user response:
Well, I still don't know why this was broken, but I did fix the problem. Maybe this will help someone in the future.
I created a new Blazor project, copied all my files into it (with relevant changes for namespaces etc), and OnAfterRender async seemed to work. Looking at the differences between the two, I noticed that the launchSettings.json files were different, so I copied the code from the newer launchSettings.json and replaced the old code with that (again, with relevant changes for namespaces etc.). Boom, things are working now in my original project.
I'm not happy about this... this is programming by coincidence and magical thinking ... but my project works again.