Skip to content

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

Closed
jonpryor wants to merge 2 commits into
feature/breakingchangesfrom
dev/jonpryor/jonp-Windows+NativeAOT+WebView2
Closed

jonpryor wants to merge 2 commits into
feature/breakingchangesfrom
dev/jonpryor/jonp-Windows+NativeAOT+WebView2

Conversation

@jonpryor

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. Conseuqently, 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.

PR Type:

🐞 Bugfix
✨ Feature

What changed? 🚀

PR Checklist ✅

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

Adds a NativeAOT-compatible WebView2 backend for the Win32 Skia runtime by introducing a WebView2Aot-based implementation for .NET 10+ while keeping the existing Microsoft.Web.WebView2 backend available (and the only option on .NET 9). This targets the Native AOT COM interop limitation reported in #23142.

Changes:

  • Split Win32 HWND/WndProc hosting logic into a new shared Win32NativeWebViewBase for reuse by multiple WebView2 backends.
  • Added Win32NativeAotWebView (NET10+) implemented via WebView2Aot, and updated backend selection via UNO_WEBVIEW2_BACKEND.
  • Updated packaging/build: conditional WebView2Aot reference (NET10+), added BannedApiAnalyzers + BannedSymbols.txt, and centralized WebView2Aot version.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Uno.UI.Runtime.Skia.Win32/Uno.UI.Runtime.Skia.Win32.csproj Adds conditional WebView2Aot dependency (NET10+), enables BannedApiAnalyzers, and includes BannedSymbols.txt as AdditionalFiles.
src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeWebViewBase.cs New shared base for Win32 child window creation/WndProc forwarding and resize handling.
src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeWebView.cs Updates provider to select backend via env var and makes the existing backend inherit the new base class.
src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeAotWebView.cs New NET10+ WebView2Aot-based backend (AOT-compatible COM interop path).
src/Uno.UI.Runtime.Skia.Win32/BannedSymbols.txt Bans DirectN.PWSTR.From(string) usage with guidance for safe alternatives.
src/Directory.Build.targets Pins WebView2Aot package version centrally.

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

@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 220289 did not find any UI Test snapshots differences.

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 220314 has failed on Uno.UI - docs.

@unodevops

Copy link
Copy Markdown
Contributor

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

@jonpryor
jonpryor force-pushed the dev/jonpryor/jonp-Windows+NativeAOT+WebView2 branch from 0ed4f5d to 72e65b9 Compare July 1, 2026 01:40
@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 220340 did not find any UI Test snapshots differences.

@mergify

mergify Bot commented Jul 1, 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

<PackageReference Update="CommunityToolkit.Mvvm" Version="$(CommunityToolkitMvvmVersion)" />
<PackageReference Update="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Update="Microsoft.Web.WebView2" Version="1.0.2592.51" Condition="'$(MSBuildProjectName)' != 'Uno.WinAppSDKSyncGenerator.References'" />
<PackageReference Update="WebView2Aot" Version="1.5.0" />

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The new package will need to be added to Uno.Sdk and uno.templates

@jonpryor jonpryor Jul 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@MartinZikmund: I don't understand "added to Uno.Sdk and uno.templates".

A .nuspec is generated for Uno.UI.Runtime.Skia.Win32 in Release builds: https://github.com/unoplatform/uno/blob/feature/breakingchanges/src/Uno.UI.Runtime.Skia.Win32/Uno.UI.Runtime.Skia.Win32.csproj#L10

		<GeneratePackageOnBuild Condition="'$(Configuration)'=='Release'">true</GeneratePackageOnBuild>

Indeed, when I build locally, the resulting generated obj/Release/Uno.WinUI.Runtime.Skia.Win32.255.255.255.255.nuspec contains:

      <group targetFramework="net10.0">
        <dependency id="Uno.WinUI.Runtime.Skia" version="255.255.255.255" exclude="Build,Analyzers" />
        <dependency id="Uno.WinUI" version="255.255.255.255" exclude="Build,Analyzers" />
        <dependency id="HarfBuzzSharp" version="8.3.1.1" exclude="Build,Analyzers" />
        <dependency id="Microsoft.Windows.CsWin32" version="0.3.106" exclude="Build,Analyzers" />
        <dependency id="Uno.icu-win" version="77.2.1" exclude="Build,Analyzers" />
        <dependency id="WebView2Aot" version="1.5.0" exclude="Build,Analyzers" />
      </group>

Is more needed?

Additionally, what needs to be done in uno.templates? Is there an example to copy?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was under the impression that there was a prop to override WebView2 version, but it seems there is not currently. In that case we can probably skip that for now, maybe it will not be necessary then.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jonpryor however, because the environment variable decides whether the AOT or non-AOT version is used, does it mean we now bundle both versions? In that case, it would probably be best to default to AOT version and have a prop in Uno.Sdk that would allow people, if they chose to, to switch to the non-AOT one, so that the choice happens before build, instead of having the option to switch at runtime

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@MartinZikmund asked:

does it mean we now bundle both versions?

Yes.

it would probably be best to default to AOT version

That is the default, which is also mentioned in the updated WebView docs: https://github.com/unoplatform/uno/blob/dev/jonpryor/jonp-Windows%2BNativeAOT%2BWebView2/doc/articles/controls/WebView.md#windows-specifics

instead of having the option to switch at runtime

The benefit to switching at runtime via environment variable is with testing: you can build once, then test either/both backends without requiring an app rebuild. (You would need to relaunch the app if setting the env var from the launching Console, but this is required anyway…)

The other benefit to the env var approach is that it adds no new public API, so we can change things without API breaks in the future.

Additionally, my hope is that for Uno 8 we drop the Microsoft.Web.WebView2 backend entirely, and use only WebView2Aot.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Runtime switching is nice for us, but I think for end users it will mean bigger assembly size, which is not ideal and is probably more important than the convenience... So if it is possible, it would be best to make sure only the selected version is bundled, via Uno.Sdk. As long as WebView2Aot seems stable, we can just keep it default and in case someone needs to, they could set some .csproj prop to swap for the old backend.

jonpryor added 2 commits July 2, 2026 08:46
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.  Conseuqently,
`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
Context: #22935

Note: `WebView.md` links to `xref:Uno.Features.NativeAOT`, which is
from #22935.
@jonpryor
jonpryor force-pushed the dev/jonpryor/jonp-Windows+NativeAOT+WebView2 branch from 72e65b9 to 3fb1fc8 Compare July 2, 2026 12:46
@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 220487 did not find any UI Test snapshots differences.

@jonpryor

jonpryor commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

#23621 -- this same idea but for master -- has been merged.

TODO: when feature/breakingchanges is updated to contain #23621, rebase this PR atop it to remove the old backend.

@jonpryor

Copy link
Copy Markdown
Contributor Author

@jonpryor wrote:

TODO: when feature/breakingchanges is updated to contain #23621, rebase this PR atop it to remove the old backend.

I forgot I had that idea, and went off and created a new PR instead: #23757.

Closing this PR.

@jonpryor jonpryor closed this Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants