Electron.NET
Electron.NET copied to clipboard
Net6 BlazorServerSide
I am trying out the Blazor ServerSide program for Net6
Follow the instructions in README
After I type "electronize Start"
The console display is as follows
Electron Socket IO Port: 8000
Electron Socket started on port 8000 at 127.0.0.1
ASP.NET Core Port: 8001
But nothing happened
What should I do, please?
Same question here
Given that those ports are often used by other apps in dev id recommend choosing different ports but otherwise just run the app directly. You've probably got some startup bug causing the dotnet app to crash.
I don't think the fix to grab the error steam from the dotnet process has been released to nuget yet so it can crash silently.
Drop your startup code. You may not be wiring something up correctly.
Drop your startup code. You may not be wiring something up correctly.
hello
Sorry for the late reply
Below is my "program.cs" file
So far the problem remains
Please take a look at it for me if you have time
`using BlazorNet6WithElectron1.Data; using ElectronNET.API; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web;
var builder = WebApplication.CreateBuilder(args); builder.Host.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseElectron(args); }); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddSingleton<WeatherForecastService>();
var app = builder.Build();
// Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); }
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub(); app.MapFallbackToPage("/_Host");
//app.Run(); Task.Run(async () => await Electron.WindowManager.CreateWindowAsync());`
Given that those ports are often used by other apps in dev id recommend choosing different ports but otherwise just run the app directly. You've probably got some startup bug causing the dotnet app to crash.
I don't think the fix to grab the error steam from the dotnet process has been released to nuget yet so it can crash silently.
hello
Sorry for the late reply
Below is my "program.cs" file
So far the problem remains
Please take a look at it for me if you have time
`using BlazorNet6WithElectron1.Data; using ElectronNET.API; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web;
var builder = WebApplication.CreateBuilder(args); builder.Host.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseElectron(args); }); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddSingleton<WeatherForecastService>();
var app = builder.Build();
// Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); }
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub(); app.MapFallbackToPage("/_Host");
//app.Run(); Task.Run(async () => await Electron.WindowManager.CreateWindowAsync());`
I'm just getting back into ElecronNET, but I did an experiment a while back on it, and my implementation was:
namespace Host {
public class Startup {
public Startup(IConfiguration configuration) {
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services) {
services.AddSingleton(ctx => {
var log = ctx.GetService<ILogger<Startup>>();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
} else {
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
if (HybridSupport.IsElectronActive) {
ElectronBootstrap();
}
}
public async void ElectronBootstrap() {
var opts = new BrowserWindowOptions {
Width = 1024,
Height = 768,
Show = false,
WebPreferences = new WebPreferences {
WebSecurity = false
}
};
var browserWindow = await Electron.WindowManager.CreateWindowAsync(opts);
await browserWindow.WebContents.Session.ClearCacheAsync();
browserWindow.OnReadyToShow += () => {
browserWindow.Show();
};
browserWindow.OnClose += () => Environment.Exit(0);
}
}
}
The biggest thing to note is that the window is being configured / shown only when it's ready to be shown, and I'm providing the window parameters immediately.
I'm trying to locate the recent bits I did in dotnet 6 to see if there's a difference from what this is.
I've got the window to render on my side. Here is how I did it:
Step 1 - Enable Electron Support
It turns-out that this code :point_down: will "freeze" the electronize start
process
builder.Host.ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseElectron(args);
});
By running the app normally (dotnet run
) I received this output message:
Building...
Unhandled exception. System.NotSupportedException: ConfigureWebHost() is not supported by WebApplicationBuilder.Host. Use the WebApplication returned by WebApplicationBuilder.Build() instead.
at Microsoft.AspNetCore.Builder.ConfigureHostBuilder.Microsoft.AspNetCore.Hosting.Infrastructure.ISupportsConfigureWebHost.ConfigureWebHost(Action`1 configure, Action`1 configureOptions)
at Microsoft.Extensions.Hosting.GenericHostWebHostBuilderExtensions.ConfigureWebHost(IHostBuilder builder, Action`1 configure, Action`1 configureWebHostBuilder)
at Microsoft.Extensions.Hosting.GenericHostWebHostBuilderExtensions.ConfigureWebHost(IHostBuilder builder, Action`1 configure)
at Microsoft.Extensions.Hosting.GenericHostBuilderExtensions.ConfigureWebHostDefaults(IHostBuilder builder, Action`1 configure)
at Program.<Main>$(String[] args) <project-directory>/Program.cs:line 14
After a bit of digging I found that the WebApplicationBuilder
class (builder
variable) has a WebHost
property which implements the IWebHostBuilder
interface. UseElectron
is an extension method for this interface.
builder.WebHost.UseElectron(args);
Step 2 - Setup The Electron Window
The Electron window setup and initialization must happen before running the server-side app
instance, otherwise it won't work.
Step 3 - Run The Server-Side App
Finally, run the server-side app after setting-up the Electron window and whatever else.
Here is my Program.cs
file:
using ElectronNET.API;
using ElectronNET.API.Entities;
using ElectronTestApp.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
// ** Step 1 - Enable Electron Support **
builder.WebHost.UseElectron(args);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
// ** Step 2 - Setup The Electron Window **
// NOTE: must happen before running the server-side app!
Task.Run(async () =>
{
// create the electron window
var browserWindow = await Electron.WindowManager.CreateWindowAsync(
new BrowserWindowOptions()
{
Height = 500,
Width = 500,
Show = false, // wait until the window is ready
WebPreferences = new()
{
WebSecurity = false
}
}
);
// initialize the window
await browserWindow.WebContents.Session.ClearCacheAsync();
// window will be rendered once its ready
browserWindow.OnReadyToShow += () => browserWindow.Show();
// handle window exit
browserWindow.OnClose += () => Environment.Exit(0);
});
// ** Step 3 - Run The Server-Side App **
app.Run();
🎉🚀 New Electron.NET version 23.6.1 released 🚀🎉
With native Electron 23 and .NET 6 support. Your problem should be fixed here. If you continue to have the problem, please let us know. Please note the correct updating of your API & CLI. Info in the README. Have fun!