Intercepting Blazor page events.
See original GitHub issueBlazor 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:
- Created 3 years ago
- Reactions:9
- Comments:19 (11 by maintainers)
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.
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. 😕