ElectronNET / Electron.NET

:electron: Build cross platform desktop apps with ASP.NET Core (Razor Pages, MVC, Blazor).
https://gitter.im/ElectronNET/community
MIT License
7.3k stars 726 forks source link

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

Closed sheapa closed 1 year ago

sheapa commented 1 year ago

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

alelom commented 1 year ago

Anyone has got any tips on how to do this?

alelom commented 1 year ago

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.

liebki commented 1 year ago

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)

mpelley commented 1 year ago

I need this also.

theolivenbaum commented 1 year ago

@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.

HalfLegend commented 1 year ago

@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?

OPGL commented 1 year ago

How i can configure Electron.NET?

johannesmols commented 1 year ago

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

andrew-locklair commented 1 year ago

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();
GregorBiswanger commented 1 year ago

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();