/ElectronNET.Blazor

A library to help Blazor and Electron.NET work better together. (Archived)

Primary LanguageC#MIT LicenseMIT

ElectronNET.Blazor

A library to help Blazor and Electron.NET work better together.

NOTE: THIS LIBRARY IS NO LONGER NECESSARY. Microsoft has fixed the underlying issues with Blazor, and everything works fine now. If you're using this library, you can uninstall it and just follow the BlazingPizza instructions. Thanks for your support!

Acknowledgements

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.

How to Use

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.

Installation Instructions:

  1. Add the https://www.nuget.org/packages/ElectronNET.Blazor NuGet package to your project.

  2. 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>();
                });

    }
  1. 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());
        }
  1. 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".

  2. Run the following command:

dotnet tool install ElectronNET.CLI -g
  1. Run the following command:
electronize init
  1. Open the Project Properties of your Server project, open the Debug tab, and under "Environment Variables", add an IS_ELECTRON variable with the value of true.

    image

  2. Next to the green arrow that starts debugging, make sure the "Electron.NET App" profile is selected, and then hit the green button.

    image

  3. PROFIT!!!