-
Notifications
You must be signed in to change notification settings - Fork 376
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
Retrieve IToastService trough IServiceProvider #2691
Comments
HI, Please supply us with ready-to-run reproduction code in the form of something we can copy/paste, a (zipped) project structure or a GitHub repository. We do not have capacity to craft or compose a reproduction for every issue that gets raised. If no code or repository is provided, this issue will be closed in 3 days Help us to help you. Thanks. |
In addition, from my experience (with FluentUI and even with MudBlazor), I had to re-inject the ToastService as a singleton class in order for it to be injected into a class that is not a Blazor component, even if the class was scoped or transient. |
Good catch @Hwiet! Closing this as it is an unsupported scenario. |
You are correct brother. It appears that AddFluentUIComponents needs to be fixed to become a singleton, making it accessible throughout the Blazor application. I have tested it thoroughly and observed no negative effects from converting it into a singleton. |
I'm re-opening this with a vNext label so we can see if it is indeed do-able to change these to singleton |
In the meantime, nothing is stopping you from creating your own |
So I indeed changed it to a AddSingleton however there is a strange behavior. I can create Toasts and remove them from another singleton class however it fails to update a Toast without error message. Now when I looked into it when updating a Toast instance if you create a new ToastParameters like below it wont update. _toastService.UpdateToast($"{task.Id}_processing", new ToastParameters<ProgressToastContent>()
{
Id = $"{task.Id}_processing",
Intent = ToastIntent.Progress,
Title = "Generating Requirements Matrix",
Timeout = 0,
Content = new ProgressToastContent()
{
Details = $"Requirements found: {task.Progress}",
},
}); The issue seems to stem from that it must be the same original ToastParameters object. For now I added a new function to at least get my use case working with adding the function below to FluentToastProvider.cs. private void UpdateToastProgressContent(string? toastId, ProgressToastContent Content)
{
_ = InvokeAsync(() =>
{
ToastInstance? toastInstance = _toastList.SingleOrDefault(x => x.Id == toastId);
if (toastInstance is not null)
{
toastInstance.Content = Content;
StateHasChanged();
};
});
} |
I've also encountered a similar issue. I have a scenario where I have to use a dedicated service scope. Example: If the resolved service has IToastService as a dependency, it won't show any messages. |
You can't just have |
I understand, but messages won't be shown if the consuming service has IToastService as a dependency and that consuming service is being resolved from a dedicated scope created somewhere in the code. I assume this is a limitation because the component responsible for rendering the messages will have a different instance of IToastService than the one created here from the dedicated scope or at least subscribe to its events. We cannot have IToastService as a singleton, and it seems designed to work within the rendered UI context scope. I've taken this approach because of the server-side simultaneous data processing between the components and EF's limitations regarding that. Still, I was looking to use the toast for error messages, but I will have to change it, and it completely makes sense to me now. |
Same problem here.. I need to access IDialogService from singleton (wasm) and if used with using (var scope = serviceProvider.CreateScope()), and as expected it throws: System.ArgumentNullException: needs to be added to the main layout of your application/site.. We need at least additional AddFluentUIComponents methods for singleton, or bool parameter in currentMethod to include as singleton.. |
That is true, @dominicusmento. A useful use case for a singleton is Wasm applications. My context is server-rendered, so it must be scoped there. |
I'm attempting to retrieve the IToastService from a singleton class through IServiceProvider using the code below. However, although I do obtain an instance of IToastService , it does not display the toast.
The text was updated successfully, but these errors were encountered: