sotsera / sotsera.blazor.toaster

A Toastr.js port to Blazor in pure .Net.
https://blazor-toaster.sotsera.com/
MIT License
130 stars 29 forks source link

Toaster from within an async / await method #23

Closed chumanevros closed 5 years ago

chumanevros commented 5 years ago

The Toaster messages do not display when called from within an async / await method.

ghidello commented 5 years ago

I can't replicate the problem: I made an async method awaiting an http call and I've been able to show a toast before and after the remote call. Can you please point me to a sample of the issue or provide the steps needed to replicate it?

chumanevros commented 5 years ago

Thank you for the response @ghidello . I think I can better explain what I have found.

I have created a test project, and it looks like when the Toaster gets injected into the class, that the reference to the UI has an issue. It may very well be how the dependency injection works or where in the life cycle this happens.

If I pass the Toaster from the index.razor page into the class, it works. If I use dependency injection with a singleton for my Test : ITest class where I inject the Toaster and then call it , it does not display the Toaster.

https://github.com/chumanevros/ToasterIssue.git

Your thoughts?

ghidello commented 5 years ago

Try registering your ITest service as scoped instead of singleton: the AddToaster method registers the IToaster service as scoped (which for client side blazor is the same as singleton) and there are issues calling a scoped service from a singleton one.

ghidello commented 5 years ago

Oh and thanks to you for taking the time for creating a test project!

chumanevros commented 5 years ago

Thank you for the response. Have tested and it works!