MudBlazor icon indicating copy to clipboard operation
MudBlazor copied to clipboard

Cannot use SnackBar within a SignalR hub.

Open A9G-Data-Droid opened this issue 6 months ago • 3 comments

Bug type

Component

Component name

ISnackbar

What happened?

I declare my snackbar as scoped:

builder.Services.AddScoped<ISnackbar, SnackbarService>();

Then I dependency inject into my SignalR hub constructor:

public class TheHub : Hub<ITheHubClient>, ITheHub
{
    private readonly ISnackbar _snackBar;

    public TheHub(ISnackbar snackBar)

The hub tracks connection and disconnection events so I can send messages to specific users:

public override Task OnConnectedAsync()

That's the thing you see happening in this error log. The error happens immediately on page load, because the connection event happens early in that flow.

Expected behavior

I would like to be able to produce SnackBar events from my Hub so that a user on the hub server web page can see messages from the clients in this way.

Let me know if I'm just going about this all wrong and backwards.

Reproduction link

https://github.com/A9G-Data-Droid/SnackbarHubMessages

Reproduction steps

  1. Blazor Server App
  2. Follow the MudBlazor install guide
  3. Create a SignalR hub
  4. Dependency inject the ISnackBar into that Hub ...

Kaboom

Relevant log output

2024-01-03 10:00:06.7565|1|ERROR|Microsoft.AspNetCore.SignalR.HubConnectionHandler|Error when dispatching 'OnConnectedAsync' on hub. System.InvalidOperationException: 'RemoteNavigationManager' has not been initialized.
   at Microsoft.AspNetCore.Components.NavigationManager.AssertInitialized()
   at Microsoft.AspNetCore.Components.NavigationManager.add_LocationChanged(EventHandler`1 value)
   at MudBlazor.SnackbarService..ctor(NavigationManager navigationManager, IOptions`1 configuration)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at lambda_method135(Closure, IServiceProvider, Object[])
   at Microsoft.AspNetCore.SignalR.Internal.DefaultHubActivator`1.Create()
   at Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher`1.OnConnectedAsync(HubConnectionContext connection)
   at Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher`1.OnConnectedAsync(HubConnectionContext connection)
   at Microsoft.AspNetCore.SignalR.HubConnectionHandler`1.RunHubAsync(HubConnectionContext connection)|url:

Version (bug)

6.12.0

Version (working)

Maybe never, it's a scoping issue

What browsers are you seeing the problem on?

Firefox, Chrome

On what operating system are you experiencing the issue?

Windows

Pull Request

  • [ ] I would like to do a Pull Request

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

A9G-Data-Droid avatar Jan 03 '24 19:01 A9G-Data-Droid