Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensions.Hosting 9.0.0 - AOT - Method not found: Void System.Text.Json.Serialization.Metadata.JsonObjectInfoValues #110214

Closed
StevenJDH opened this issue Nov 27, 2024 · 2 comments

Comments

@StevenJDH
Copy link

Description

After upgrading the Microsoft.Extensions.Hosting nuget from 8.0.1 to 9.0.0, I get the following error when running the executable compiled via AOT using source generation:

Unhandled Exception: System.MissingMethodException: Method not found: 'Void System.Text.Json.Serialization.Metadata.JsonObjectInfoValues1.set_ConstructorAttributeProviderFactory(System.Func1<System.Reflection.ICustomAttributeProvider>)'

The error does not happen with the previous versions, nor does it happen when compiled to a non-AOT executable using the latest version.

Reproduction Steps

Create the following minimalistic example project to test.

BugExample.csproj

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <PublishAot>true</PublishAot>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
    </ItemGroup>
</Project>

Program.cs

using System.Text.Json;
using System.Text.Json.Serialization;

Console.WriteLine("Hello, World!");

var example = new Example("Test");
string json = JsonSerializer.Serialize(example, SourceGenerationContext.Default.Example);

Console.WriteLine("Result: {0}", json);

public record Example(string Foobar);

[JsonSourceGenerationOptions(WriteIndented = false)]
[JsonSerializable(typeof(Example))]
internal partial class SourceGenerationContext : JsonSerializerContext { }

Then run the following commands, assuming you are on Windows:

dotnet publish -r win-x64 -c Release --property:PublishDir=./bin/Publish
cd bin\Publish
BugExample.exe

Expected behavior

The output should look like it does when using version 8.0.1:

EXPECTED OUTPUT

Hello, World!
Result: {"Foobar":"Test"}

Actual behavior

Serialization fails with method not found:

ACTUAL OUTPUT

Hello, World!
Unhandled Exception: System.MissingMethodException: Method not found: 'Void System.Text.Json.Serialization.Metadata.JsonObjectInfoValues`1<Example>.set_ConstructorAttributeProviderFactory(System.Func`1<System.Reflection.ICustomAttributeProvider>)'.
   at Internal.Runtime.TypeLoaderExceptionHelper.CreateMissingMethodException(ExceptionStringID, String) + 0x40
   at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowMissingMethodException(ExceptionStringID, String) + 0x9
   at SourceGenerationContext.Create_Example(JsonSerializerOptions) + 0x15
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoNoCaching(Type) + 0x39
   at System.Text.Json.JsonSerializerOptions.CachingContext.CreateCacheEntry(Type type, JsonSerializerOptions.CachingContext context) + 0x1d
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Text.Json.JsonSerializerOptions.CachingContext.CacheEntry.GetResult() + 0x1e
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type, Boolean, Nullable`1, Boolean, Boolean) + 0x3c
   at System.Text.Json.JsonSerializerOptions.GetTypeInfo(Type) + 0x4c
   at SourceGenerationContext.get_Example() + 0x36
   at Program.<Main>$(String[] args) + 0x4b
   at BugExample!<BaseAddress>+0x153960

Regression?

The closest issue I could find from the past that was somewhat related to this was #61737. Seems to be something now that has changed with JsonObjectInfoValues in the latest version.

Known Workarounds

Use 8.0.1 instead of 9.0.0.

Configuration

  • .NET 8
  • Windows 11 and Linux (Ubuntu)
  • x64
  • It's not specific to this configuration as far as I can tell.

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 27, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky
Copy link
Member

This is same root cause as #109872. The fix will probably be available in January (we already fixed it in main). Only workaround right now is to target .NET 9.

@MichalStrehovsky MichalStrehovsky closed this as not planned Won't fix, can't repro, duplicate, stale Nov 27, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants