A library to help Blazor and Electron.NET work better together.
Special thanks to the Electron.NET team for building an amazing resource, and to Maher Jendoubi for setting me down the path to making this work with his article on adding Electron.NET to BlazingPizza.
Electron.NET can be applied to any Blazor app that uses the "Blazor (ASP.NET Core Hosted)" template. This is because there has to be something that serves up the web page to the Chrome instance, and .NET Core MVC apps are just compiled EXEs anyway. Standalone apps won't work because there won't be any built-in way to serve up the resources.
-
Add the https://www.nuget.org/packages/ElectronNET.Blazor NuGet package to your project.
-
Modify the Program.cs of your Server app to look like this:
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.UseElectron(args)
.UseStartup<Startup>();
});
}
- Modify the Startup.cs file of your Server app to look like this (pay attention, the registration order is important):
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseResponseCompression();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBlazorDebugging();
}
app.UseElectronNETStaticFiles<Client.Startup>();
app.UseClientSideBlazorFiles<Client.Startup>();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapFallbackToClientSideElectronNET<Client.Startup>("index.html");
});
Task.Run(async () => await Electron.WindowManager.CreateWindowAsync());
}
-
Open Windows Explorer, navigate to the root folder of your solution, then hold down the Shift key while right-clicking that folder, and select "Open Powershell window here".
-
Run the following command:
dotnet tool install ElectronNET.CLI -g
- Run the following command:
electronize init
-
Open the Project Properties of your Server project, open the Debug tab, and under "Environment Variables", add an
IS_ELECTRON
variable with the value oftrue
. -
Next to the green arrow that starts debugging, make sure the "Electron.NET App" profile is selected, and then hit the green button.
-
PROFIT!!!