Update Readme to add guidance when using .NET 6 Minimal API Program.cs #713

Closed
opened 2026-01-29 16:46:30 +00:00 by claunia · 4 comments
Owner

Originally created by @schaveyt on GitHub (Oct 17, 2021).

.NET 6 templates are now using the minimal APIs syntax by default. However the existing IWebBuilder extension method is not compatible as the new WebApplicationBuilder ASP.NET class

I propose adding an additional guidance to the Readme:

If using a Program.cs using the new Minimal API of .NET 6 add the single line as noted below

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseElectron(args);  // add this line here
...
...
Originally created by @schaveyt on GitHub (Oct 17, 2021). .NET 6 templates are now using the minimal APIs syntax by default. However the existing IWebBuilder extension method is not compatible as the new WebApplicationBuilder ASP.NET class I propose adding an additional guidance to the Readme: > If using a Program.cs using the new Minimal API of .NET 6 add the single line as noted below ~~~java var builder = WebApplication.CreateBuilder(args); builder.WebHost.UseElectron(args); // add this line here ... ... ~~~
claunia added the Feature label 2026-01-29 16:46:30 +00:00
Author
Owner

@schaveyt commented on GitHub (Oct 18, 2021):

Actually, I am now finding this only works when running electronize start /watch as the /watch option start the .net process using the dotnet watch run style. When one attempts to run as electronize start, ElectronNET.API's UseElectron extension method attempts to change the ContentRoot and goes 💥 and throw the following exception:

Unhandled exception. System.NotSupportedException: The content root changed. Changing the host configuration is not supported.
   at Microsoft.AspNetCore.Builder.ConfigureWebHostBuilder.UseSetting(String key, String value)
   at Microsoft.AspNetCore.Hosting.HostingAbstractionsWebHostBuilderExtensions.UseContentRoot(IWebHostBuilder hostBuilder, String contentRoot)
   at ElectronNET.API.WebHostBuilderExtensions.UseElectron(IWebHostBuilder builder, String[] args)
   at Program.<Main>$(String[] args) in C:\tmp\test-app\Program.cs:line 13

This is due to this line her of the ElectronNET.API:
c2a8c627b9/ElectronNET.API/WebHostBuilderExtensions.cs (L46)

There, does, in fact appear to be a change in behavior for .net6 when using the Minimal.API vs the .net5 style startup as evidence here on the github issue: https://github.com/dotnet/aspnetcore/issues/36876#issuecomment-925894121 by @davidfowl

Base on this and the his related pull request, it does look as if Minimal API can support the current approach. I even tried what one of asp.net test did with no luck:

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    ContentRootPath = AppDomain.CurrentDomain.BaseDirectory
});
@schaveyt commented on GitHub (Oct 18, 2021): Actually, I am now finding this only works when running `electronize start /watch` as the /watch option start the .net process using the `dotnet watch run` style. When one attempts to run as `electronize start`, ElectronNET.API's UseElectron extension method attempts to change the ContentRoot and goes 💥 and throw the following exception: ~~~ Unhandled exception. System.NotSupportedException: The content root changed. Changing the host configuration is not supported. at Microsoft.AspNetCore.Builder.ConfigureWebHostBuilder.UseSetting(String key, String value) at Microsoft.AspNetCore.Hosting.HostingAbstractionsWebHostBuilderExtensions.UseContentRoot(IWebHostBuilder hostBuilder, String contentRoot) at ElectronNET.API.WebHostBuilderExtensions.UseElectron(IWebHostBuilder builder, String[] args) at Program.<Main>$(String[] args) in C:\tmp\test-app\Program.cs:line 13 ~~~ This is due to this line her of the ElectronNET.API: https://github.com/ElectronNET/Electron.NET/blob/c2a8c627b923cc85126ceb065fb631ba1f4faadb/ElectronNET.API/WebHostBuilderExtensions.cs#L46 There, does, in fact appear to be a change in behavior for .net6 when using the Minimal.API vs the .net5 style startup as evidence here on the github issue: https://github.com/dotnet/aspnetcore/issues/36876#issuecomment-925894121 by @davidfowl Base on this and the his related pull request, it does look as if Minimal API can support the current approach. I even tried what one of asp.net test did with no luck: ~~~csharp var builder = WebApplication.CreateBuilder(new WebApplicationOptions { ContentRootPath = AppDomain.CurrentDomain.BaseDirectory }); ~~~
Author
Owner

@schaveyt commented on GitHub (Oct 18, 2021):

I did confirm that running the .net6 SDK command does still works with ElectronNET as it generates the traditional Program.cs and Startup.cs 👍 :

dotnet new blazorserver --framework net5.0 -o MyStellerApp.Server
@schaveyt commented on GitHub (Oct 18, 2021): I did confirm that running the .net6 SDK command does still works with ElectronNET as it generates the traditional Program.cs and Startup.cs 👍 : ~~~bash dotnet new blazorserver --framework net5.0 -o MyStellerApp.Server ~~~
Author
Owner

@schaveyt commented on GitHub (Oct 18, 2021):

😡 Just discovered that the amazing hot reload feature in dotnet watch only works when no arguments are passed to it.

c7c4e5d11b/src/BuiltInTools/dotnet-watch/Program.cs (L299)

Only the following two command will permit the hot reload to activate:

dotnet watch
dotnet watch run

This impacts the experience for ElectronNET users, as the electronize start /watch call dotnet like this:

dotnet watch run --environment=Development /electronPort=8000 /electronWebPort=8001
@schaveyt commented on GitHub (Oct 18, 2021): 😡 Just discovered that the amazing hot reload feature in `dotnet watch` only works when no arguments are passed to it. https://github.com/dotnet/sdk/blob/c7c4e5d11b910997f2a723d7f84d214f265ba2a4/src/BuiltInTools/dotnet-watch/Program.cs#L299 Only the following two command will permit the hot reload to activate: ~~~bash dotnet watch dotnet watch run ~~~ This impacts the experience for ElectronNET users, as the `electronize start /watch` call dotnet like this: ~~~bash dotnet watch run --environment=Development /electronPort=8000 /electronWebPort=8001 ~~~
Author
Owner

@schaveyt commented on GitHub (Dec 28, 2021):

Withdrawing this change request.

@schaveyt commented on GitHub (Dec 28, 2021): Withdrawing this change request.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#713