Skip to content

App throws TypeLoadException when published trimmed but works fine when published native AOT (no trim warnings) #102796

@DamianEdwards

Description

@DamianEdwards

Description

Repo: https://github.com/DamianEdwards/RazorSlices/tree/aot/samples/RazorSlices.Samples.WebApp

I'm updating my Razor Slices library to support native AOT and am running into a scenario where the sample app produces no warnings when published as trimmed or as native AOT, and runs successfully when published native AOT, but throws a TypeLoadException when running after publishing trimmed.

The issue appears to be an interface implementation being trimmed from a type that's generated by a source generator. The method in question is a static abstract interface member. Notably, this method is not called via the interface in a generic fashion anywhere, which differs from other generated types in this app using the same pattern. I'm assuming this is the cause of the issue.

image

image
 System.TypeLoadException: Type 'RazorSlices.Samples.WebApp.Slices._Layout' from assembly 'RazorSlices.Samples.WebApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' tried to override method 'Create' but does not implement or inherit that method.
         at AspNetCoreGeneratedDocument.Slices_Todos.GetLayout()
         at RazorSlices.RazorSlice.RenderToBufferWriterAsync(IBufferWriter`1 bufferWriter, Func`2 flushAsync, HtmlEncoder htmlEncoder, CancellationToken cancellationToken) in D:\src\GitHub\DamianEdwards\RazorSlices\src\RazorSlices\RazorSlice.cs:line 118
         at RazorSlices.RazorSlice.RenderAsync(IBufferWriter`1 bufferWriter, Func`2 flushAsync, HtmlEncoder htmlEncoder, CancellationToken cancellationToken) in D:\src\GitHub\DamianEdwards\RazorSlices\src\RazorSlices\RazorSlice.cs:line 87
         at RazorSlices.RazorSlicePipeWriterExtensions.RenderToPipeWriterAsync(RazorSlice, PipeWriter, HtmlEncoder , CancellationToken ) in D:\src\GitHub\DamianEdwards\RazorSlices\src\RazorSlices\RazorSlice.PipeWriterExtensions.cs:line 21
         at RazorSliceHttpResultHelpers.ExecuteAsync(RazorSlice, HttpContext, HtmlEncoder , Int32, String) in D:\src\GitHub\DamianEdwards\RazorSlices\src\RazorSlices\RazorSliceHttpResultHelpers.cs:line 22
         at Microsoft.AspNetCore.Http.HttpResults.RazorSliceHttpResult`1.Microsoft.AspNetCore.Http.IResult.ExecuteAsync(HttpContext httpContext) in D:\src\GitHub\DamianEdwards\RazorSlices\src\RazorSlices\RazorSliceHttpResultOfTModel.cs:line 34
         at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>F69328E0708B4B584C5AACA22FE2C51A1CF192D6622828F613FC57C583CA77B63__GeneratedRouteBuilderExtensionsCore.ExecuteAsyncExplicit(IResult, HttpContext) in D:\src\GitHub\DamianEdwards\RazorSlices\samples\RazorSlices.Samples.WebApp\Microsoft.AspNetCore.Http.RequestDelegateGenerator\Microsoft.AspNetCore.Http.RequestDelegateGenerator.RequestDelegateGenerator\GeneratedRouteBuilderExtensions.g.cs:line 1075
         at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>F69328E0708B4B584C5AACA22FE2C51A1CF192D6622828F613FC57C583CA77B63__GeneratedRouteBuilderExtensionsCore.<>c__DisplayClass3_0.<MapGet1>g__RequestHandler|4(HttpContext httpContext) in D:\src\GitHub\DamianEdwards\RazorSlices\samples\RazorSlices.Samples.WebApp\Microsoft.AspNetCore.Http.RequestDelegateGenerator\Microsoft.AspNetCore.Http.RequestDelegateGenerator.RequestDelegateGenerator\GeneratedRouteBuilderExtensions.g.cs:line 203
         at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
         at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1)

Note that if the app is also configured to publish self-contained, the error message does not include a description which makes it much harder to diagnose the root cause:

Connection id "0HN3VJ6M4UE06", Request id "0HN3VJ6M4UE06:00000001": An unhandled exception was thrown by the application.
      System.TypeLoadException
         at AspNetCoreGeneratedDocument.Slices_Todos.GetLayout()
         at RazorSlices.RazorSlice.RenderToBufferWriterAsync(IBufferWriter`1 bufferWriter, Func`2 flushAsync, HtmlEncoder htmlEncoder, CancellationToken cancellationToken) in D:\src\GitHub\DamianEdwards\RazorSlices\src\RazorSlices\RazorSlice.cs:line 118
         at RazorSlices.RazorSlice.RenderAsync(IBufferWriter`1 bufferWriter, Func`2 flushAsync, HtmlEncoder htmlEncoder, CancellationToken cancellationToken) in D:\src\GitHub\DamianEdwards\RazorSlices\src\RazorSlices\RazorSlice.cs:line 87
         at RazorSlices.RazorSlicePipeWriterExtensions.RenderToPipeWriterAsync(RazorSlice, PipeWriter, HtmlEncoder , CancellationToken ) in D:\src\GitHub\DamianEdwards\RazorSlices\src\RazorSlices\RazorSlice.PipeWriterExtensions.cs:line 21
         at RazorSliceHttpResultHelpers.ExecuteAsync(RazorSlice, HttpContext, HtmlEncoder , Int32, String) in D:\src\GitHub\DamianEdwards\RazorSlices\src\RazorSlices\RazorSliceHttpResultHelpers.cs:line 22
         at Microsoft.AspNetCore.Http.HttpResults.RazorSliceHttpResult`1.Microsoft.AspNetCore.Http.IResult.ExecuteAsync(HttpContext httpContext) in D:\src\GitHub\DamianEdwards\RazorSlices\src\RazorSlices\RazorSliceHttpResultOfTModel.cs:line 34
         at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>FCB64269D8A4C26C2E072E32ACF06A96ADD7E2DEE99BD0C9B78931C09283D15A4__GeneratedRouteBuilderExtensionsCore.ExecuteAsyncExplicit(IResult, HttpContext) in D:\src\GitHub\DamianEdwards\RazorSlices\samples\RazorSlices.Samples.WebApp\obj\Release\net8.0\win-x64\generated\Microsoft.AspNetCore.Http.RequestDelegateGenerator\Microsoft.AspNetCore.Http.RequestDelegateGenerator.RequestDelegateGenerator\GeneratedRouteBuilderExtensions.g.cs:line 1075
         at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>FCB64269D8A4C26C2E072E32ACF06A96ADD7E2DEE99BD0C9B78931C09283D15A4__GeneratedRouteBuilderExtensionsCore.<>c__DisplayClass3_0.<MapGet1>g__RequestHandler|4(HttpContext httpContext) in D:\src\GitHub\DamianEdwards\RazorSlices\samples\RazorSlices.Samples.WebApp\obj\Release\net8.0\win-x64\generated\Microsoft.AspNetCore.Http.RequestDelegateGenerator\Microsoft.AspNetCore.Http.RequestDelegateGenerator.RequestDelegateGenerator\GeneratedRouteBuilderExtensions.g.cs:line 203
         at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
         at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1)

Reproduction Steps

  1. Clone https://github.com/DamianEdwards/RazorSlices and checkout the aot branch
  2. Navigate to .\samples\RazorSlices.Samples.WebApp\
  3. Run dotnet publish to publish the app as native AOT
  4. Run .\bin\Release\net8.0\win-x64\publish\RazorSlices.Samples.WebApp.exe to run the published app
  5. Open a browser and navigate to http://localhost:5000/ and click around the pages of the site, seeing it works
  6. Stop the app
  7. Edit the RazorSlices.Samples.WebApp.csproj file so that <PublishAot>true</PublishAot> is commented out and <PublishTrimmed>true</PublishTrimmed>, e.g.:
    <!--<PublishAot>true</PublishAot>-->
    <PublishTrimmed>true</PublishTrimmed>
    <!--<PublishSingleFile>true</PublishSingleFile>-->

8.Run dotnet publish to publish the app trimmed
9. Run .\bin\Release\net8.0\win-x64\publish\RazorSlices.Samples.WebApp.exe to run the published app
10. Open a browser and navigate to http://localhost:5000/
11. Note the exception message in the terminal the app was run from

Expected behavior

As not trim warnings are produced on publish, and the app works as expected when published native AOT, I expect it to work when published trimmed.

Actual behavior

The app throws an exception at runtime after being published trimmed.

Regression?

Not sure but it repros with both SDK versions 8.0.106 and 8.0.300 on Windows x64

Known Workarounds

None

Configuration

Windows 11 x64
.NET SDK 8.0.106 and 8.0.300

Other information

No response

Metadata

Metadata

Assignees

Labels

area-Tools-ILLink.NET linker development as well as trimming analyzers

Type

No type

Projects

Status

No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions