question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Intercepting Blazor page events.

See original GitHub issue

Blazor supports the notion of events, such as @onlick events, as shown here:

<button class="btn btn-danger" @onclick="DeleteUser">Delete</button>

This allows the DeleteUser method to be invoked in the page’s @code section.

I’m looking for a mechanism that allows intercepting calls those ‘code behind’ methods, in order to be able to execute some infrastructure logic right before the method is invoked. If such mechanism is currently missing, I would urge the addition of a feature that makes this possible.

This question/discussion is related to my earlier issues #19642 and #29194 because I’m trying to find ways to integrate non-conforming DI Containers (such as Simple Injector) with the Blazor pipeline. As non-conforming containers don’t replace the built-in DI Container, but merely live side-by-side, it is important to be able to start or continue a non-conforming container’s Scope at the proper time.

Starting and continuing an existing scope can be done partially by:

  • intercepting the creation of Blazor components (using IComponentActivator) to start/continue a scope
  • intercepting the creation of SignalR hubs (using IHubActivator<T>) to start/continue a scope

This unfortunately leaves us with the invocation of Blazor events. When they are invoked, neither the IComponentActivator nor IHubActivator<T> is called, which causes that code to be executed outside the context of a non-conforming container’s scope.

I might have overlooked the proper interception point for this in the Blazor code base. If there is such an interception point, please let me know. If there isn’t, I would like to see it added.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:9
  • Comments:19 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
inf9144commented, Jan 13, 2023

To solve this in a local project (not a solution for frameworks) you can use Castle.DynamicProxy / IInterceptor together with an custom implementation of IComponentFactory. You can easily intercept OnInitialized(Async) OnParametersSet(Async) OnAfterRender(Async) and if you pass IHandleEvent as additional interface you can intercept IHandleEvent.HandleEventAsync and get access to all event delegates that get executed for your component 😃

Never the less - it would be much easier and more performant if there would be inbuild support to do sth like this.

2reactions
inf9144commented, Jan 11, 2023

Would also like to have this. If you could intercept all delegates (or their invocations) that get passed to EventCallbackFactory you could do things like structured exception handling or advanced logging or other aspects. Right now you need to boilerplate everything that needs to go in every event handler. 😕

Read more comments on GitHub >

github_iconTop Results From Across the Web

Detecting navigation events
Detecting navigation events ... Access to browser navigation from Blazor is provided via the NavigationManager service. This can be injected into a Blazor...
Read more >
ASP.NET Core Blazor routing and navigation
This article explains how to manage Blazor app request routing and how to use the NavLink component to create navigation links.
Read more >
Blazor's New LocationChanging Events in .NET 7
The new LocationChanging event runs just before Blazor navigates to a new “page.” To use it, you need an instance of NavigationManager ....
Read more >
Controlling and Preventing Navigation Events in Blazor Using ...
Clicking within the bounds of the page first however means that the event will be intercepted. As you can see in the above...
Read more >
What happens when you navigate to the "same page" in ...
Blazor intercepts all attempts to navigate 'within' your app. In practice this means, rather than dive off to a server to locate a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found