Configuration for Program.cs in a .NET 6 project without Startup.cs #834

Closed
opened 2026-01-29 16:49:47 +00:00 by claunia · 10 comments
Owner

Originally created by @sheapa on GitHub (Nov 2, 2022).

Originally assigned to: @GregorBiswanger on GitHub.

The documentation does not explain how to configure the Program.cs file for new .NET 6 projects that don't have Startup.cs.

Originally created by @sheapa on GitHub (Nov 2, 2022). Originally assigned to: @GregorBiswanger on GitHub. The documentation does not explain how to configure the Program.cs file for new .NET 6 projects that don't have Startup.cs.
claunia added the Feature label 2026-01-29 16:49:47 +00:00
Author
Owner

@alelom commented on GitHub (Nov 17, 2022):

Anyone has got any tips on how to do this?

@alelom commented on GitHub (Nov 17, 2022): Anyone has got any tips on how to do this?
Author
Owner

@alelom commented on GitHub (Nov 17, 2022):

Found an example here: https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60-samples?view=aspnetcore-7.0#customize-iwebhostbuilder

Which explains how to convert the WebHostBuilder, but not the
app.Run(async () => await Electron.WindowManager.CreateWindowAsync()); of https://github.com/ElectronNET/Electron.NET#startupcs.

@alelom commented on GitHub (Nov 17, 2022): Found an example here: https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60-samples?view=aspnetcore-7.0#customize-iwebhostbuilder Which explains how to convert the WebHostBuilder, but not the `app.Run(async () => await Electron.WindowManager.CreateWindowAsync());` of https://github.com/ElectronNET/Electron.NET#startupcs.
Author
Owner

@liebki commented on GitHub (Nov 17, 2022):

Honestly, it didn't work I fiddled with it for about three days, then I just ported it to MAUI 👀. (It, is a project, which used ElectronNet)

@liebki commented on GitHub (Nov 17, 2022): Honestly, it didn't work I fiddled with it for about three days, then I just ported it to MAUI 👀. (It, is a project, which used ElectronNet)
Author
Owner

@mpelley commented on GitHub (Nov 24, 2022):

I need this also.

@mpelley commented on GitHub (Nov 24, 2022): I need this also.
Author
Owner

@theolivenbaum commented on GitHub (Nov 24, 2022):

@sheapa if you use this fork, you can do something like this:

Electron.ReadAuth(); //Must be first thing, to read the auth key necessary for the Electron socket comms
var webport = Electron.Experimental.FreeTcpPort();
var socketport = int.Parse(BridgeSettings.SocketPort);
foreach (var arg in args)
{
    if (arg.ToUpper().Contains("ELECTRONPORT"))
    {
        socketport = int.Parse(arg.ToUpper().Replace("/ELECTRONPORT=", ""));
    }
    else if (arg.ToUpper().Contains("ELECTRONWEBPORT"))
    {
        webport = int.Parse(arg.ToUpper().Replace("/ELECTRONWEBPORT=", ""));
    }
}
BridgeSettings.InitializePorts(socketport, webport); //As we do not use UseElectron, we must manually do this here

This will hopefully be merged back to the main repo in the future.

@theolivenbaum commented on GitHub (Nov 24, 2022): @sheapa if you use [this fork](https://github.com/theolivenbaum/Electron.NET), you can do something like this: ```csharp Electron.ReadAuth(); //Must be first thing, to read the auth key necessary for the Electron socket comms var webport = Electron.Experimental.FreeTcpPort(); var socketport = int.Parse(BridgeSettings.SocketPort); foreach (var arg in args) { if (arg.ToUpper().Contains("ELECTRONPORT")) { socketport = int.Parse(arg.ToUpper().Replace("/ELECTRONPORT=", "")); } else if (arg.ToUpper().Contains("ELECTRONWEBPORT")) { webport = int.Parse(arg.ToUpper().Replace("/ELECTRONWEBPORT=", "")); } } BridgeSettings.InitializePorts(socketport, webport); //As we do not use UseElectron, we must manually do this here ``` This will hopefully be merged back to the main repo in the future.
Author
Owner

@HalfLegend commented on GitHub (Dec 7, 2022):

@sheapa if you use this fork, you can do something like this:

Electron.ReadAuth(); //Must be first thing, to read the auth key necessary for the Electron socket comms
var webport = Electron.Experimental.FreeTcpPort();
var socketport = int.Parse(BridgeSettings.SocketPort);
foreach (var arg in args)
{
    if (arg.ToUpper().Contains("ELECTRONPORT"))
    {
        socketport = int.Parse(arg.ToUpper().Replace("/ELECTRONPORT=", ""));
    }
    else if (arg.ToUpper().Contains("ELECTRONWEBPORT"))
    {
        webport = int.Parse(arg.ToUpper().Replace("/ELECTRONWEBPORT=", ""));
    }
}
BridgeSettings.InitializePorts(socketport, webport); //As we do not use UseElectron, we must manually do this here

This will hopefully be merged back to the main repo in the future.

It works!! Thanks
When will you merge it?

@HalfLegend commented on GitHub (Dec 7, 2022): > @sheapa if you use [this fork](https://github.com/theolivenbaum/Electron.NET), you can do something like this: > > ```cs > Electron.ReadAuth(); //Must be first thing, to read the auth key necessary for the Electron socket comms > var webport = Electron.Experimental.FreeTcpPort(); > var socketport = int.Parse(BridgeSettings.SocketPort); > foreach (var arg in args) > { > if (arg.ToUpper().Contains("ELECTRONPORT")) > { > socketport = int.Parse(arg.ToUpper().Replace("/ELECTRONPORT=", "")); > } > else if (arg.ToUpper().Contains("ELECTRONWEBPORT")) > { > webport = int.Parse(arg.ToUpper().Replace("/ELECTRONWEBPORT=", "")); > } > } > BridgeSettings.InitializePorts(socketport, webport); //As we do not use UseElectron, we must manually do this here > ``` > > This will hopefully be merged back to the main repo in the future. It works!! Thanks When will you merge it?
Author
Owner

@OPGL commented on GitHub (Jan 1, 2023):

How i can configure Electron.NET?

@OPGL commented on GitHub (Jan 1, 2023): How i can configure Electron.NET?
Author
Owner

@johannesmols commented on GitHub (Jan 16, 2023):

This worked for me: https://github.com/ElectronNET/Electron.NET/issues/685#issuecomment-1252042885

@johannesmols commented on GitHub (Jan 16, 2023): This worked for me: https://github.com/ElectronNET/Electron.NET/issues/685#issuecomment-1252042885
Author
Owner

@andrew-locklair commented on GitHub (Feb 24, 2023):

Hi all,

I have this minimal example working with a .NET web application, using top-level statements and no Startup.cs. You shouldn't have to do manual handling to sync Electron, you can just use the WebHost from builder. Here's what it looks like:

using ElectronNET.API;

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseElectron(args);

var app = builder.Build();

app.MapGet("/", () => "Hello World");

if (HybridSupport.IsElectronActive)
{
    Task.Run(async () =>
    {
        BrowserWindow window = await Electron.WindowManager.CreateWindowAsync();
        window.OnReadyToShow += () => window.Show();
        window.OnClosed += () => Electron.App.Quit();
    });
}

app.Run();
@andrew-locklair commented on GitHub (Feb 24, 2023): Hi all, I have this minimal example working with a .NET web application, using top-level statements and no Startup.cs. You shouldn't have to do manual handling to sync Electron, you can just use the WebHost from builder. Here's what it looks like: ``` using ElectronNET.API; var builder = WebApplication.CreateBuilder(args); builder.WebHost.UseElectron(args); var app = builder.Build(); app.MapGet("/", () => "Hello World"); if (HybridSupport.IsElectronActive) { Task.Run(async () => { BrowserWindow window = await Electron.WindowManager.CreateWindowAsync(); window.OnReadyToShow += () => window.Show(); window.OnClosed += () => Electron.App.Quit(); }); } app.Run(); ```
Author
Owner

@GregorBiswanger commented on GitHub (Feb 24, 2023):

Here's an example from a Blazor Server side app using Electron.NET:

using electron7_demo.Data;
using ElectronNET.API;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.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");
    // 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");

await app.StartAsync();
await Electron.WindowManager.CreateWindowAsync();

app.WaitForShutdown();
@GregorBiswanger commented on GitHub (Feb 24, 2023): Here's an example from a Blazor Server side app using Electron.NET: ``` using electron7_demo.Data; using ElectronNET.API; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; var builder = WebApplication.CreateBuilder(args); builder.WebHost.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"); // 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"); await app.StartAsync(); await Electron.WindowManager.CreateWindowAsync(); app.WaitForShutdown(); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#834