Skip to content

How can a backend service communicate with a Razor Component? #8509

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

Closed
keithwill opened this issue Mar 14, 2019 · 3 comments
Closed

How can a backend service communicate with a Razor Component? #8509

keithwill opened this issue Mar 14, 2019 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components question

Comments

@keithwill
Copy link

keithwill commented Mar 14, 2019

What is the proper way to let a background process/task raise an event or notification that a Razor Component can consume?

For example, if I have a background queue processor, it might make changes to a record through a service class, and the service could raise an event or notification that the entity has changed.

Using Angular or other SPA frameworks, it was pretty easy to listen for SignalR hub messages with notifications from the service which would signal to the component to either apply the record change, or signal to the component to pull fresh data.

With Razor Components there is no SignalR connection needed to send messages, as they would all be handled server side (only the rendering is passed to the browser), but there should be a generic way to send messages to a component to enable the same kind of functionality you would have using a JS SPA framework with SignalR.

Is there, or could there be an IComponentHubContext that could be injected into backend services that would let you invoke methods on all component instances (much as you would invoke a SignalR method on a IHubContext) and it would handle calling using the appropriate execution/thread context (as opposed to running it on the thread that the service making the call is currently running on). On Blazor it could push the call to the client to execute, over the existing SignalR hub connection.

Maybe it could be constrained to calling event handler methods in the component, since such calls wouldn't be able to wait for or use the results of the methods if they had a return type.

@mkArtakMSFT mkArtakMSFT added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-blazor Includes: Blazor, Razor Components question labels Mar 14, 2019
@mkArtakMSFT
Copy link
Contributor

Thanks for contacting us, @keithwill.
It's actually much simpler with Razor Components in ASP.NET Core. You can inject a service you want to listen to directly into the component using the @inject directive and subscribe to the event of your interest. When the event fires, the component will handle it and should call the StateHasChanged() method to indicate that a UI update is required. That will cause the runtime to do the needful and send UI updates to the browser.

@keithwill
Copy link
Author

keithwill commented Mar 14, 2019

So the events would need to be async delegates if the component event handler needs to do any database or network IO to avoid causing substantial delays in user perception of the event propagation, and its probably not appropriate to do computationally expensive operations in the event handler.

Thanks!

@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@kempcalalo
Copy link

@mkArtakMSFT can you give code snippets as an example? We're trying to do the exact same thing but isn't able to to so. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components question
Projects
None yet
Development

No branches or pull requests

3 participants