/positron

Electron alternative using all .NET technologies

Primary LanguageC#

Positron

Proof-of-concept Electron alternative using only .NET technologies.

Components

Blazor WebAssembly

ASP.NET Core Static File Server

WebAssembly applications are static files that do not need any complex servers or server-side features. Kestrel can be used as a static file server to accomplish this.

Include WebAssembly artifacts as embedded resources

The Blazor WebAssembly application published artifacts will be added as embedded resources in the ASP.NET Core project. To preserve folder heirarchy, a ManifestEmbeddedFileProvider will be used instead of the EmbeddedFileProvider.

<PropertyGroup>
    <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>

Use the Microsoft.Extensions.FileProviders.Embedded NuGet package.

Disabling executable generation

Disable generation of the executable for ASP.NET Core projects by adding the following to the csproj:

<PropertyGroup>
  <UseAppHost>false</UseAppHost>
</PropertyGroup>

See https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#useapphost

.NET 5 WPF Application

Using the Generic Host with WPF

public partial class App : Application
{
    private readonly IHost _host;

    public App()
    {
        _host = Host.CreateDefaultBuilder()
            .ConfigureHost()
            .Build();
    }

    protected override async void OnStartup(StartupEventArgs e)
    {
        await _host.StartAsync();

        var mainWindow = _host.Services.GetRequiredService<MainWindow>();
        mainWindow.Show();

        base.OnStartup(e);
    }

    protected override async void OnExit(ExitEventArgs e)
    {
        using (_host)
        {
            await _host.StopAsync(TimeSpan.FromSeconds(5));
        }

        base.OnExit(e);
    }
}

Self-hosting Kestrel as a background service

The BackgroundWebHostService implements the IHostedService interface for background services. This class manages the IWebHost implementation in the background; the lifetime of the web host should completely overlap the lifetime of the WPF application.

WebView2 Browser Component

Javascript Interop

  1. Javascript Post message is bound to a CQRS request object
  2. Request is handled by Mediatr
  3. A JSON response is asynchronously posted back to the DOM

UWP and WinRT

Desktop Bridge

Detecting WinRT environment

References