-
Notifications
You must be signed in to change notification settings - Fork 0
Console App
A major benefit in ElectronNET.Core is the ability to build Electron applications using simple console applications instead of requiring ASP.NET Core. This removes a significant barrier and enables many more use cases.
Console applications with ElectronNET.Core support multiple content scenarios:
- File System HTML/JS - Serve static web content directly from the file system
- Remote Server Integration - Connect to existing web servers or APIs
- Lightweight Architecture - Avoid ASP.NET overhead when not needed
- Simplified Deployment - Package and distribute with minimal dependencies
See System Requirements.
Create a new console application in Visual Studio by selecting New Project and choosing one of the project templates for console apps.
dotnet new console -n MyElectronApp
cd MyElectronAppdotnet add package ElectronNET.CoreNote
The API package is automatically included as a dependency of ElectronNET.Core.
Add the Electron.NET configuration to your .csproj file:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ElectronNET.Core" Version="1.0.0" />
</ItemGroup>Warning
Specifying OutputType property is crucial in order to get the ability of WSL debugging. Especially it is not included in ASP.NET projects.
When you migrate from ASP.NET to a console application, be sure to add this to the project file.
Here's a complete console application example:
using System;
using System.Threading.Tasks;
using ElectronNET.API.Entities;
namespace MyElectronApp
public class Program
{
public static async Task Main(string[] args)
{
var runtimeController = ElectronNetRuntime.RuntimeController;
try
{
// Start Electron runtime
await runtimeController.Start();
await runtimeController.WaitReadyTask;
// Initialize your Electron app
await InitializeApp();
// Wait for shutdown
await runtimeController.WaitStoppedTask.ConfigureAwait(false);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
await runtimeController.Stop().ConfigureAwait(false);
await runtimeController.WaitStoppedTask.WaitAsync(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
}
}
private static async Task InitializeApp()
{
// Create main window
var browserWindow = await Electron.WindowManager.CreateWindowAsync(
new BrowserWindowOptions
{
Show = false,
WebPreferences = new WebPreferences
{
// Add these two when using file:// URLs
WebSecurity = false,
AllowRunningInsecureContent = true,
NodeIntegration = false,
ContextIsolation = true
}
});
// Load your content (file system, remote URL, etc.)
await browserWindow.WebContents.LoadURLAsync("https://example.com");
// Show window when ready
browserWindow.OnReadyToShow += () => browserWindow.Show();
}
}Serve HTML/JS files from your project:
// In your project root, create wwwroot/index.html
var fileInfo = new FileInfo(Environment.ProcessPath);
var exeFolder = fileInfo.DirectoryName;
var htmlPath = Path.Combine(exeFolder, "wwwroot/index.html");
var url = new Uri(htmlPath, UriKind.Absolute);
await browserWindow.WebContents.LoadFileAsync(url.ToString());Load content from any web server:
await browserWindow.WebContents.LoadURLAsync("https://your-server.com/app");- Debugging - Learn about debugging console applications
- Package Building - Create distributable packages
- Migration Guide - Moving from ASP.NET projects
✅ Simpler Architecture - No ASP.NET complexity when not needed
✅ Flexible Content - Use any HTML/JS source
✅ Faster Development - Less overhead for simple applications
✅ Easy Deployment - Minimal dependencies
✅ Better Performance - Lighter weight than full web applications
Want to contribute to this documentation? Please fork and create a PR! The Wiki is autogenerated from the /docs content in the repository.