Skip to content

Stack overflow when trying to process ICoreWebView2 within Microsoft.Web.WebView2.Core. #1695

Open

Description

Actual behavior

Stack overflow when trying to process Microsoft.Web.WebView2.Core.winmd.

Expected behavior

No stack overflow.

Repro steps

  1. Create the project:

    dotnet new console -n webview2-cswin32
    cd webview2-cswin32
    dotnet package add Microsoft.Web.WebView2
    dotnet package add Microsoft.Windows.CsWin32
    
  2. Update webview2-cswin32.csproj to add @(ProjectionMetadataWinmd) for Microsoft.Web.WebView2.Core.winmd:

    <ItemGroup>
      <!-- update this, adding %(GeneratePathProperty) -->
      <PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3967.48" GeneratePathProperty="true" />
    </ItemGroup>
    
    <ItemGroup>
      <ProjectionMetadataWinmd Include="$(PkgMicrosoft_Web_WebView2)\lib\Microsoft.Web.WebView2.Core.winmd" />
    </ItemGroup>
  3. NativeMethods.txt content:

    ICoreWebView2
    
  4. NativeMethods.json content:

    {
      "$schema": "https://aka.ms/CsWin32.schema.json",
      "comInterop": {
        "useComSourceGenerators": true
      },
      "emitSingleFile": false
    }

Please see this project: webview2-cswin32.zip

It may build the first time:

> dotnet build
…
    …\webview2-cswin32\NativeMethods.txt(1,1): warning PInvoke001: Method, type or constant "ICoreWebView2" not found

Subsequent builds will fail:

> donet build
…
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error Stack overflow.
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at System.Runtime.EH.DispatchEx(System.Runtime.StackFrameIterator ByRef, ExInfo ByRef)
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.Windows.CsWin32.Generator.<IsManagedType>g__Helper|352_0(System.Reflection.Metadata.TypeDefinitionHandle, <>c__DisplayClass352_0 ByRef)
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.Windows.CsWin32.Generator.IsManagedType(System.Reflection.Metadata.TypeDefinitionHandle)
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.Windows.CsWin32.Generator.IsManagedType(Microsoft.Windows.CsWin32.TypeHandleInfo)
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.Windows.CsWin32.HandleTypeHandleInfo.ToTypeSyntax(Microsoft.Windows.CsWin32.TypeSyntaxSettings, GeneratingElement, System.Nullable`1<Microsoft.Windows.CsWin32.QualifiedCustomAttributeHandleCollection>, System.Reflection.ParameterAttributes)
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.Windows.CsWin32.Generator.<IsManagedType>g__Helper|352_0(System.Reflection.Metadata.TypeDefinitionHandle, <>c__DisplayClass352_0 ByRef)
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.Windows.CsWin32.Generator.IsManagedType(System.Reflection.Metadata.TypeDefinitionHandle)
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.Windows.CsWin32.Generator.IsManagedType(Microsoft.Windows.CsWin32.TypeHandleInfo)
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.Windows.CsWin32.HandleTypeHandleInfo.ToTypeSyntax(Microsoft.Windows.CsWin32.TypeSyntaxSettings, GeneratingElement, System.Nullable`1<Microsoft.Windows.CsWin32.QualifiedCustomAttributeHandleCollection>, System.Reflection.ParameterAttributes)
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.Windows.CsWin32.Generator.<IsManagedType>g__Helper|352_0(System.Reflection.Metadata.TypeDefinitionHandle, <>c__DisplayClass352_0 ByRef)
…
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.CodeAnalysis.CommandLine.BuildClient.Run(System.Collections.Generic.IEnumerable`1<System.String>, Microsoft.CodeAnalysis.CommandLine.RequestLanguage, Microsoft.CodeAnalysis.CommandLine.CompileFunc, Microsoft.CodeAnalysis.CommandLine.CompileOnServerFunc, Microsoft.CodeAnalysis.CommandLine.ICompilerServerLogger)
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.CodeAnalysis.CSharp.CommandLine.Program.MainCore(System.String[])
    C:\Program Files\dotnet\sdk\10.0.203\Roslyn\Microsoft.CSharp.Core.targets(89,5): error    at Microsoft.CodeAnalysis.CSharp.CommandLine.Program.Main(System.String[])

Build failed with 764 error(s) and 1 warning(s) in 6.0s

Context

  • CsWin32 version: 0.3.275
  • Target Framework: net10.0

Activity

  1. jonpryor commented on May 14, 2026

    @jonpryor
    Author

    Stack trace of the stack overflow shows Helper() calling IsManagedType(), which is presumably here:

    return qtdh.Generator.IsManagedType(qtdh.DefinitionHandle);

  2. jevansaks commented on May 20, 2026

    @jevansaks
    Member

    So that winmd is a WinRT one and CsWin32 is designed only to process Win32Metadata WinMDs. I recognize this is confusing. I can update the tooling to give a better error, but you want to use the CsWinRT projections for that nuget package (which are embedded in that package and you should be getting via nuget package reference already).

    FYI Manodasan Wignarajah (@manodasanW) Sergio Pedri (@Sergio0694)

  3. jonpryor commented on May 20, 2026

    @jonpryor
    Author

    Jevan Saks (@jevansaks) wrote:

    So that winmd is a WinRT one and CsWin32 is designed only to process Win32Metadata WinMDs. I recognize this is confusing.

    Indeed it is confusing. (There are multiple types of .winmd files?!)

    "Obvious question is obvious": is there a Win32Metadata WinMD for WebView2? I don't see one in the Microsoft.Web.Webview2 1.0.3967.48 package, which does have bindings/projections/whatever for .NET Framework + WPF and WinForms in addition to WinRT, and nothing about the path lib/Microsoft.Web.WebView2.Core.winmd implies that it's specific to WinRT (especially when compared to e.g. lib/net462/Microsoft.Web.WebView2.Wpf.dll, which is clearly not WinRT).

    I guess I should look into using microsoft/win32metadata or something to turn WebView2.tlb/WebView2.idl into a Win32Metadata .winmd file?

  4. jevansaks commented on May 20, 2026

    @jevansaks
    Member

    So I'm not sure what your goal is, but if you look in the WebView2 nuget package under lib_manual you'll see the "projection" dlls which is where the C#/WinRT versions of these interfaces live and that's the recommended way to call/implement WinRT interfaces such as ICoreWebView2.

    Keep in mind ICoreWebView2 is an "exclusive" interface on the CoreWebView2 runtimeclass so that means it's not intended for you to implement, it's only for calling. In C#/WinRT you'd interact with "CoreWebView2" directly.

  5. smourier commented on May 25, 2026

    @smourier

    "Obvious question is obvious": is there a Win32Metadata WinMD for WebView2? I don't see one in the Microsoft.Web.Webview2 1.0.3967.48 package, which does have bindings/projections/whatever for .NET Framework + WPF and WinForms in addition to WinRT, and nothing about the path lib/Microsoft.Web.WebView2.Core.winmd implies that it's specific to WinRT (especially when compared to e.g. lib/net462/Microsoft.Web.WebView2.Wpf.dll, which is clearly not WinRT).

    Jonathan Pryor (@jonpryor) FWIW, I maintain one here https://github.com/smourier/WebView2Aot/tree/main/WebView2Aot.InteropBuilder.Cli (no relation to Winforms/WPF nor to CsWin32) built using this project https://github.com/smourier/webview2-win32md.

  6. littletoxic commented on Jun 30, 2026

    @littletoxic
    Contributor

    Jonathan Pryor (@jonpryor) The WebView2 "projection" assemblies appear to be the AOT-compatible path. The key property is WebView2EnableCsWinRTProjection; when enabled, the WebView2 package references Microsoft.Web.WebView2.Core.Projection.dll / WinRT.Runtime instead of the classic Microsoft.Web.WebView2.Core.dll COM interop path.

    Here is a small Win32 + WebView2 Native AOT sample using that approach: https://github.com/littletoxic/WebView2Browser

    This probably is not a drop-in fix for Uno, since Uno currently has its own Microsoft.Web.WebView2.Core.* API surface and enabling the projection directly may cause type conflicts. But it seems like a useful direction for a dedicated Win32 Native AOT WebView implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions