Skip to content

feat(win32): WebView2Aot backend for <WebView2/> - #23621

Merged
jonpryor merged 1 commit into
masterfrom
dev/jonpryor/jonp-Windows+NativeAOT+WebView2-master
Jul 6, 2026
Merged

jonpryor merged 1 commit into
masterfrom
dev/jonpryor/jonp-Windows+NativeAOT+WebView2-master

Conversation

@jonpryor

@jonpryor jonpryor commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Fixes: #23142

Context: MicrosoftEdge/WebView2Feedback#5238
Context: https://github.com/smourier/WebView2Aot
Context: smourier/DirectNAot#9

On Windows, the XAML control is implemented in terms of the Microsoft.Web.WebView2 NuGet package.

The problem: Microsoft.Web.WebView2, when used for "normal" .NET, uses COM Interop. Native AOT does not support COM interop. The result is that things fail, a'la
MicrosoftEdge/WebView2Feedback#5238 (which is for WinForms, but the same fundamental idea).

In the case of #23142, if we build Uno.Gallery for Native AOT:

git clone https://github.com/unoplatform/Uno.Gallery.git
cd Uno.Gallery
git checkout 54a08b1587aa020a5651d61eb3a3023a79e89d72
sed -i '' 's/"Uno.Sdk": ".*"/"Uno.Sdk": "6.6.0-dev.216"/g' global.json
dotnet publish -c Release -r win-arm64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop `
  -bl Uno.Gallery/Uno.Gallery.csproj -p:SelfContained=true -p:UseSkiaRendering=true `
  -p:SkiaPublishAot=true -p:IsAotCompatible=true

Run it:

.\Uno.Gallery\bin\Release\net10.0-desktop\win-arm64\publish\Uno.Gallery.exe | Out-Default

and access the UI components > WebView entry in the left tree, then the console contains the following output:

fail: Uno.UI.Dispatching.NativeDispatcher[0]
      NativeDispatcher unhandled exception
      System.NotSupportedException: COM Interop requires ComWrapper instance registered for marshalling.
         at System.Runtime.InteropServices.ComWrappers.ComInterfaceForObject(Object) + 0xa4
         at System.Runtime.InteropServices.ComWrappers.ComInterfaceForObject(Object, Guid) + 0x24
         at Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateCoreWebView2EnvironmentWithOptions(String, String, ICoreWebView2EnvironmentOptions, ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler) + 0xdc
         at Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateAsync>d__102.MoveNext() + 0xe4
      --- End of stack trace from previous location ---
         at Uno.UI.Runtime.Skia.Win32.Win32NativeWebView.<>c__DisplayClass15_0.<<-ctor>b__0>d.MoveNext() + 0x60
      --- End of stack trace from previous location ---
         at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__124_0(Object state) + 0x1c
         at Uno.UI.Dispatching.NativeDispatcher.RunAction(NativeDispatcher, Action) + 0x8c

The Microsoft.Web.WebView2 package does support Native AOT, but only if you're building for WinRT. (Uno is not built on WinRT.)

A way to use Microsoft.Web.WebView2 while supporting Native AOT in a "normal" (non-WinRT) Windows environment is to instead use smourier/WebView2Aot, in the WebView2Aot NuGet package. WebView2Aot uses the DirectN AOT generator to emit Native AOT- compatible COM interop infrastrucure.

Update Uno.UI.Runtime.Skia.Win32 as follows:

  1. Split out the "native" bits of Win32NativeWebView into Win32NativeWebViewBase.cs. (Anything dealing with a Windows WndProc, etc.)

  2. Update Win32NativeWebView to inherit Win32NativeWebViewBase.

  3. Add a new Win32NativeAotWebView type, which also inherits from Win32NativeWebViewBase. This implements <WebView2/> support in terms of WebView2Aot.

However, WebView2Aot requires .NET 10. Consequently, Uno.UI.Runtime.Skia.Win32.csproj should only reference WebView2Aot when targeting .NET 10 and later, and Win32NativeAotWebView can only exist in .NET 10 and later.

Additionally, this is a potential significant change. We want WebView2Aot to be the default in all Windows configurations for consistency -- nobody wants to debug a scenario of "WebView2 works when I'm not building for Native AOT!" -- but there should be a way to use the previous Microsoft.Web.WebView2 implementation in case WebView2Aot breaks things, or for testing, or…

Update Win32NativeWebViewProvider as follows:

  1. When built for .NET 9, only Win32NativerWebView / the Microsoft.Web.WebView2 backend is supported.

  2. When built for .NET 10+, the new WebView2Aot backend is used by default. This can be overridden by setting the UNO_WEBVIEW2_BACKEND environment variable to microsoft.web.webview2.

    UNO_WEBVIEW2_BACKEND can also be set to webview2aot to be explicit.

The UNO_WEBVIEW2_BACKEND environment variable is always read, and will log an error message when using an unsupported value, e.g. when UNO_WEBVIEW2_BACKEND=webview2aot on .NET 9.

Aside: add BannedApiAnalyzers to Uno.UI.Runtime.Skia.Win32.csproj which make it an error to use PWSTR.From(string). See also smourier/DirectNAot#9.

GitHub Issue: closes #

PR Type:

What changed? 🚀

PR Checklist ✅

Copilot AI review requested due to automatic review settings July 2, 2026 15:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a NativeAOT-compatible WebView2 backend for the Win32 Skia runtime by integrating WebView2Aot (NET10+) and selecting it by default on .NET 10+, while keeping the existing Microsoft.Web.WebView2 backend as a fallback via UNO_WEBVIEW2_BACKEND.

Changes:

  • Add a shared Win32 HWND/WndProc hosting base (Win32NativeWebViewBase) and refactor the existing WebView2 implementation to use it.
  • Introduce a new NET10+ Win32NativeAotWebView backend based on WebView2Aot, with backend selection via UNO_WEBVIEW2_BACKEND.
  • Update packaging/docs and add banned API analyzer configuration for unsafe PWSTR.From(string) usage.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/Uno.UI.Runtime.Skia.Win32/Uno.UI.Runtime.Skia.Win32.csproj Conditionally references WebView2Aot for net10+ and enables banned API analyzer with BannedSymbols.txt.
src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeWebViewBase.cs New base type factoring Win32 window creation, WndProc, and resize forwarding for WebView hosting.
src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeWebView.cs Adds backend selection logic and refactors existing Microsoft.Web.WebView2 implementation to derive from the new base.
src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeAotWebView.cs New NET10+ WebView2Aot-based backend implementation.
src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeAotWebView.Request.cs AOT wrappers for WebResourceRequested request/headers iteration against WebView2Aot COM interfaces.
src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeAotWebView.Response.cs AOT wrappers for WebResourceRequested response/headers against WebView2Aot COM interfaces.
src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeAotWebView.Helpers.cs Stream conversion helpers and a generated COM IStream implementation for request/response bodies.
src/Uno.UI.Runtime.Skia.Win32/BannedSymbols.txt Bans DirectN.PWSTR.From(string) with guidance for a fixed-pointer alternative.
src/Directory.Build.targets Pins WebView2Aot package version repository-wide.
doc/articles/controls/WebView.md Documents the two Windows WebView2 backends and the UNO_WEBVIEW2_BACKEND override.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread doc/articles/controls/WebView.md Resolved
@unodevops

Copy link
Copy Markdown
Contributor

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 220515 has failed on Uno.UI - CI.

@ajpinedam ajpinedam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few valid comments from Copilot but aside from that it looks good

Fixes: #23142

Context: MicrosoftEdge/WebView2Feedback#5238
Context: https://github.com/smourier/WebView2Aot
Context: smourier/DirectNAot#9

On Windows, the <WebView2/> XAML control is implemented in terms of
the [Microsoft.Web.WebView2 NuGet package][0].

The problem: Microsoft.Web.WebView2, when used for "normal" .NET,
uses COM Interop.  Native AOT does not support COM interop.
The result is that things fail, a'la
MicrosoftEdge/WebView2Feedback#5238 (which is for WinForms, but the
same fundamental idea).

In the case of #23142, if we build Uno.Gallery for Native AOT:

	git clone https://github.com/unoplatform/Uno.Gallery.git
	cd Uno.Gallery
	git checkout 54a08b1587aa020a5651d61eb3a3023a79e89d72
	sed -i '' 's/"Uno.Sdk": ".*"/"Uno.Sdk": "6.6.0-dev.216"/g' global.json
	dotnet publish -c Release -r win-arm64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop `
	  -bl Uno.Gallery/Uno.Gallery.csproj -p:SelfContained=true -p:UseSkiaRendering=true `
	  -p:SkiaPublishAot=true -p:IsAotCompatible=true

Run it:

	.\Uno.Gallery\bin\Release\net10.0-desktop\win-arm64\publish\Uno.Gallery.exe | Out-Default

and access the **UI components > WebView** entry in the left tree,
then the console contains the following output:

	fail: Uno.UI.Dispatching.NativeDispatcher[0]
	      NativeDispatcher unhandled exception
	      System.NotSupportedException: COM Interop requires ComWrapper instance registered for marshalling.
	         at System.Runtime.InteropServices.ComWrappers.ComInterfaceForObject(Object) + 0xa4
	         at System.Runtime.InteropServices.ComWrappers.ComInterfaceForObject(Object, Guid) + 0x24
	         at Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateCoreWebView2EnvironmentWithOptions(String, String, ICoreWebView2EnvironmentOptions, ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler) + 0xdc
	         at Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateAsync>d__102.MoveNext() + 0xe4
	      --- End of stack trace from previous location ---
	         at Uno.UI.Runtime.Skia.Win32.Win32NativeWebView.<>c__DisplayClass15_0.<<-ctor>b__0>d.MoveNext() + 0x60
	      --- End of stack trace from previous location ---
	         at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__124_0(Object state) + 0x1c
	         at Uno.UI.Dispatching.NativeDispatcher.RunAction(NativeDispatcher, Action) + 0x8c

The Microsoft.Web.WebView2 package *does* support Native AOT, but
*only if* you're building for WinRT.  (Uno is not built on WinRT.)

A way to use Microsoft.Web.WebView2 while supporting Native AOT in a
"normal" (non-WinRT) Windows environment is to instead use
smourier/WebView2Aot, in the [WebView2Aot NuGet package][1].
WebView2Aot uses the [DirectN AOT generator][2] to emit Native AOT-
compatible COM interop infrastrucure.

Update `Uno.UI.Runtime.Skia.Win32` as follows:

 1. Split out the "native" bits of `Win32NativeWebView` into
    `Win32NativeWebViewBase.cs`.  (Anything dealing with a
    Windows WndProc, etc.)

 2. Update `Win32NativeWebView` to inherit `Win32NativeWebViewBase`.

 3. Add a new `Win32NativeAotWebView` type, which also inherits from
    `Win32NativeWebViewBase`.  This implements `<WebView2/>` support
    in terms of WebView2Aot.

However, WebView2Aot requires .NET 10.  Consequently,
`Uno.UI.Runtime.Skia.Win32.csproj` should only reference WebView2Aot
when targeting .NET 10 and later, and `Win32NativeAotWebView` can
only exist in .NET 10 and later.

Additionally, this is a potential significant change.  We want
WebView2Aot to be the default in all Windows configurations for
consistency -- nobody wants to debug a scenario of "WebView2 works
when I'm not building for Native AOT!" -- but there should be a way
to use the previous Microsoft.Web.WebView2 implementation in case
WebView2Aot breaks things, or for testing, or…

Update `Win32NativeWebViewProvider` as follows:

 1. When built for .NET 9, only Win32NativerWebView / the
    Microsoft.Web.WebView2 backend is supported.

 2. When built for .NET 10+, the new WebView2Aot backend is used by
    default.  This can be overridden by setting the
    `UNO_WEBVIEW2_BACKEND` environment variable to
    `microsoft.web.webview2`.

    `UNO_WEBVIEW2_BACKEND` can also be set to `webview2aot` to be
    explicit.

The `UNO_WEBVIEW2_BACKEND` environment variable is always read, and
will log an error message when using an unsupported value, e.g. when
`UNO_WEBVIEW2_BACKEND=webview2aot` on .NET 9.

Aside: add BannedApiAnalyzers to `Uno.UI.Runtime.Skia.Win32.csproj`
which make it an error to use `PWSTR.From(string)`.
See also smourier/DirectNAot#9.

[0]: https://www.nuget.org/packages/Microsoft.Web.WebView2
[1]: https://www.nuget.org/packages/WebView2Aot
[2]: https://github.com/smourier/DirectNAot
@jonpryor
jonpryor force-pushed the dev/jonpryor/jonp-Windows+NativeAOT+WebView2-master branch from 07b81aa to 89e6d00 Compare July 6, 2026 12:51
@unodevops

Copy link
Copy Markdown
Contributor

@mergify

mergify Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@jonpryor
jonpryor merged commit 7184be1 into master Jul 6, 2026
67 checks passed
@jonpryor
jonpryor deleted the dev/jonpryor/jonp-Windows+NativeAOT+WebView2-master branch July 6, 2026 16:18
jonpryor added a commit that referenced this pull request Jul 14, 2026
Context: #23621
Context: #23603

PR #23621 added a WebView2Aot backend for `<WebView2/>` within
`Uno.UI.Runtime.Skia.Win32`, and added support for a
`UNO_WEBVIEW2_BACKEND` environment variable to switch between the
backends.

For Uno 7, remove support for the original Microsoft.Web.WebView2
backend.  We'll stick with WebView2Aot going forward, which will
reduce assembly size (only one backend), simplify maintenance
(only one backend), and has support for Native AOT.

Additionally, as the feature/breakingchanges branch is now on .NET 11
(#23603), which drops support for .NET 9, we no longer need to make
the WebView2Aot NuGet reference conditional on .NET 10+.
Cleanup `Uno.UI.Runtime.Skia.Win32` accordingly.
jonpryor added a commit that referenced this pull request Jul 15, 2026
Context: #23621
Context: #23603

PR #23621 added a WebView2Aot backend for `<WebView2/>` within
`Uno.UI.Runtime.Skia.Win32`, and added support for a
`UNO_WEBVIEW2_BACKEND` environment variable to switch between the
backends.

For Uno 7, remove support for the original Microsoft.Web.WebView2
backend.  We'll stick with WebView2Aot going forward, which will
reduce assembly size (only one backend), simplify maintenance
(only one backend), and has support for Native AOT.

Additionally, as the feature/breakingchanges branch is now on .NET 11
(#23603), which drops support for .NET 9, we no longer need to make
the WebView2Aot NuGet reference conditional on .NET 10+.
Cleanup `Uno.UI.Runtime.Skia.Win32` accordingly.
@jpnurmi jpnurmi mentioned this pull request Sep 23, 2026
1 of 2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

4 participants