neggenbe
neggenbe

Reputation: 1885

Electron.Net window not opening in blazor server app (.Net 6.0)

I am trying to create a new Blazor server application working with Electron.Net on .Net 6.0.

Unlike .Net 5.0 and previous, app configuration and startup code is now done in Program.cs class, as explained here.

What is unclear to me at this stage is how to convert the required Electron.Net lines in .Net 6.0.

From the documentation, we should add the following lines

// -- in Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseElectron(args); // <= THIS LINE
        webBuilder.UseStartup<Startup>();
    });

// -- in Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...

    // Open the Electron-Window here
    Task.Run(async () => await Electron.WindowManager.CreateWindowAsync());
}

Now in .Net 6.0 we have only the Program.cs class remaining, so these two lines should be converted to :

// -- Program.cs .Net 6.0
public static void Main(string[] args) {
    var builder = WebApplication.CreateBuilder(args);

    // --- Add electron...
    builder.WebHost.UseElectron(args);
    ....
    // -- run the app
    app.Run();
    // --- if running Electron - should window be created here - this way?
    Task.Run(async () => await Electron.WindowManager.CreateWindowAsync());
}

I then run the application as electronize start /PublishSingleFile false

Output shows app is running but the Electron window is not showing up. Here the console output:

ElectronHostHook handling started...
Invoke electron.cmd - in dir: C:\myProject\obj\Host\node_modules\.bin
electron.cmd "..\..\main.js"

Electron Socket IO Port: 8000
Electron Socket started on port 8000 at 127.0.0.1
ASP.NET Core Port: 8001
stdout: info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:5001

stdout: info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.

stdout: info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\myProject\obj\Host\bin\

And navigating in my brower to https://localhost:5001 shows the running app. As it seems, electron is not running properly despite being started.

Upvotes: 3

Views: 1745

Answers (2)

neggenbe
neggenbe

Reputation: 1885

Just making clear the main method may NOT be async, which leads to this code :

// --- Add electron...
builder.Services.AddElectron();
builder.WebHost.UseElectron(args);
if (HybridSupport.IsElectronActive) {
    // Open the Electron-Window here
    Task.Run(async () => {
        var window = await Electron.WindowManager.CreateWindowAsync();
        window.OnClosed += () => {
        Electron.App.Quit();
      };
    });
}

Upvotes: 1

Delonous
Delonous

Reputation: 201

Think I figured it out. Need all these for it to work.

builder.Services.AddElectron();
builder.WebHost.UseElectron(args);
if (HybridSupport.IsElectronActive)
{
    var window = await Electron.WindowManager.CreateWindowAsync();
    window.OnClosed += () =>
    {
        Electron.App.Quit();
    };
}

Upvotes: 1

Related Questions