aspire-webview2-wpf 1.0.0

dotnet add package aspire-webview2-wpf --version 1.0.0
                    
NuGet\Install-Package aspire-webview2-wpf -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="aspire-webview2-wpf" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="aspire-webview2-wpf" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="aspire-webview2-wpf" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add aspire-webview2-wpf --version 1.0.0
                    
#r "nuget: aspire-webview2-wpf, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package aspire-webview2-wpf@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=aspire-webview2-wpf&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=aspire-webview2-wpf&version=1.0.0
                    
Install as a Cake Tool

Aspire WebView2 Loader in WPF

Load Apsire host and one web ui in pwf with webview2.

Example

  • .csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="aspire-webview2-wpf" Version="1.0.0-preview2s" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0-rc.2.25502.107" />
    <PackageReference Include="Serilog.Extensions.Hosting" Version="9.0.1-dev-02307" />
    <PackageReference Include="Serilog.Sinks.Console" Version="6.0.1-dev-00953" />
  </ItemGroup>
  
  <ItemGroup>
    <None Update="appsettings.*.json">
      <DependentUpon>appsettings.json</DependentUpon>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>
  • Program.cs
using aspire_webview2_wpf;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;

try
{
    using var cts = new CancellationTokenSource();
    Console.CancelKeyPress += (_, _) => cts.Cancel();

    var builder = Host.CreateApplicationBuilder();
    builder.Services.ConfigureAspireWebview2WpfLoader();
    builder.Services.AddHostedService<HostedService>();
    builder.Services.AddSerilog(logger =>
    {
        logger.WriteTo.Console();
    });

    var app = builder.Build();
    await app.RunAsync(cts.Token);
}
catch (Exception exp)
{
    Console.Error.WriteLine(exp);
}
  • HostedService.cs
using aspire_webview2_wpf;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

internal sealed class HostedService(
    IAspireWebview2WpfLoader uiLoader,
    IHostApplicationLifetime lifetime,
    IConfiguration configuration,
    ILogger<HostedService> logger)
    : IHostedService
{
    public async Task StartAsync(CancellationToken cancellationToken)
    {
        try
        {
            var aspireProjectPath = configuration.GetValue<string?>("aspireProjectPath");
            if (string.IsNullOrWhiteSpace(aspireProjectPath))
            {
                logger.LogWarning("invalid configuration: missing `aspireProjectPath`");
                Environment.ExitCode = -1;
                return;
            }
            var settings = new AspireWebview2WpfLoaderSettings(
                title: "Aspire WebView2 Loader Sample",
                icon: "assets/icon.ico",
                keepConsole: true);
            await uiLoader.Run(aspireProjectPath, settings, cancellationToken: cancellationToken);
        }
        catch (Exception exp)
        {
            logger.LogError(exp, "");
        }
        finally
        {
            lifetime.StopApplication();
        }
    }

    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
  • Whne use default IAspireUrlResolver, print out The UI address is: http(s)://YOUR-WEB-UI-ADDRESS-HERE in aspire host project:
var builder = DistributedApplication.CreateBuilder(args);
// . . .
builder.Eventing.Subscribe<ResourceReadyEvent>((e, c) =>
{
    switch (e.Resource.Name)
    {
        case "UI_PROJECT_NAME_HERE":
            if (e.Resource.TryGetUrls(out var urls))
            {
                foreach (var uri in urls)
                {
                    if (!string.IsNullOrWhiteSpace(uri.Url))
                    {
                        Console.WriteLine($"The UI address is: {uri.Url}");
                        break;
                    }
                }
            }
            break;
        default: break;
    }
    return Task.CompletedTask;
});
Product Compatible and additional computed target framework versions.
.NET net9.0-windows7.0 is compatible.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 213 10/22/2025