I'm using a server-side Blazor in an Electron.Net app. I created an application menu in a C# class with an on-click event to open a dialog and it does not work. I suspect the problem is the scope of the injected DialogService and the dependency on the NavigationManager. When using the DialogService in a Blazor page, it works, but not from my standard C# class.
Hi @NmsPLavoie,
Welcome to the Radzen community!
To be honest we haven't tested with Electron.NET. You are probably right and some service may not be provided. Is there any stacktrace available? By the way did you add a <RadzenDialog />
to your layout similar to this?
No stacktrace because no error, it just does nothing. Yes, I have the RadzenDialog and I confirm it works by using the DialogService directly in a Blazor page.
Tried it with our online demos. The dialog page didn't work initially because SQLIte failed to initialize. However after removing all DB related code it worked as expected.
Thank you for the quick reply. If used directly in a Blazor page, it works. But if you try to call it from the ApplicationMenu, it doesn't.
Unfortunately I am not sure how to test that.
In Setup.Configure(), add :
Electron.Menu.SetApplicationMenu(new[]
{
new MenuItem
{
Label = "DialogService",
Submenu = new[]
{
new MenuItem
{
Label = "Test",
Click = () => DialogService.Open<TestPage>("Test")
}
}
}
});
Where is the DialogService coming from? Is it the same instance as the one injected? You may have to get it at runtime from the service collection.
I don't know how to get a proper instance of the DialogService. When I try to get it from DI within Setup.Configure(), I'm receiving an error. I tried new DialogService() or differ the DI later on the app lifecycle with no error, but nothing happens. I tried to configure it with AddSingleton or AddScoped, even created a scope before DI it, but nothing works.
I tried the code you provided but it doesn't seem to execute no matter what is in the Click handler. Tried with await Electron.Dialog.ShowMessageBoxAsync("Test");
and it didn't show.
Electron.Dialog.ShowMessageBoxAsync("Test")
works for me in the AppMenu. I'm on Windows, so I'm not sure how is supposed to work on MacOS.
Throwing an exception doesn't work for me either. Anyway my idea is that you need to get an instance of the DialogService which is the same as the one provided in the Blazor application. I wanted to try with:
var dialogService = app.ApplicationServices.GetService<DialogService>();
but couldn't because no code was executed when I clicked the menu item.
Instantiating a new service won't work for sure. It has to be the one provided to the Blazor app.
A possible workaround would be to try the IPC - the Blazor app will listen to an event sent on menu click and invoke the DialogService.
I reproduced the problem with the LatestBlazor project : https://send.firefox.com/download/de91a9c8f8e8e8c5/#kcbTowajLz8YovtfGToPVQ
Injecting DialogService dialogService into Startup.Configure trigger that exception :
Application startup exception
System.Exception: Could not resolve a service of type 'Radzen.DialogService' for the parameter 'dialogService' of method 'Configure' on type 'LatestBlazor.Startup'.
---> System.InvalidOperationException: 'RemoteNavigationManager' has not been initialized.
at Microsoft.AspNetCore.Components.NavigationManager.AssertInitialized()
at Microsoft.AspNetCore.Components.NavigationManager.add_LocationChanged(EventHandler1 value) at Radzen.DialogService..ctor(NavigationManager uriHelper) at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) 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 singletonCallSite, 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__DisplayClass1_0.b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.b__2(IApplicationBuilder app)
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
Unfortunately I've hit this one before: https://github.com/aspnet/AspNetCore/issues/13906. No solution provided - scopes are different. I am out of ideas but to use Electron IPC and open the dialog from Blazor (where it works) after receiving a message from the menu click.