Skip to content

System.MissingMethodException: Method not found: 'Void System.Text.Json.Serialization.Metadata.JsonObjectInfoValues1.set_ObjectCreator(System.Func1<!0>)'. with .NET Standard 2.0 #61737

@hugoqribeiro

Description

@hugoqribeiro

Description

I have a .NET 6 app that references a .NET Standard 2.0 class library, which in turn uses the new System.Text.Json source generator to speed up serialization.

When running I get the following exception (this stack trace is from a project used to reproduce the error):

System.MissingMethodException
  HResult=0x80131513
  Message=Method not found: 'Void System.Text.Json.Serialization.Metadata.JsonObjectInfoValues`1.set_ObjectCreator(System.Func`1<!0>)'.
  Source=ClassLibrary1
  StackTrace:
   at ClassLibrary1.SerializerContext.get_MyModel() in D:\PrjNET\_Experiments\SystemTextJsonSerializerContext\ClassLibrary1\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SerializerContext.MyModel.g.cs:line 37
   at ClassLibrary1.Serializer.Serialize(MyModel model) in D:\PrjNET\_Experiments\SystemTextJsonSerializerContext\ClassLibrary1\Serializer.cs:line 9
   at Program.<Main>$(String[] args) in D:\PrjNET\_Experiments\SystemTextJsonSerializerContext\ConsoleApp1\Program.cs:line 5

This DOES NOT happen if the class library targets .NET 6.

Reproduction Steps

I've created a sample to reproduce this so I could eliminate the possibility of something being wrong with the original projects (upgraded from .NET 5).

The class library (netstandard2.0) code:

namespace ClassLibrary1
{
    public class MyModel
    {
        public string Value
        {
            get; set;
        }
    }

    public partial class Serializer
    {
        public static string Serialize(MyModel model)
        {
            return JsonSerializer.Serialize(
                model,
                SerializerContext.Default.MyModel);
        }

        public static string SerializeWithGenerics<T>(T model)
        {
            return JsonSerializer.Serialize(
                model,
                typeof(T),
                SerializerContext.Default);
        }
    }

    [JsonSerializable(typeof(MyModel))]
    internal partial class SerializerContext : JsonSerializerContext
    {
    }
}

The console app (net6.0) code:

using ClassLibrary1;

MyModel model1 = new MyModel();

string json1 = Serializer.Serialize(model1);
string json2 = Serializer.SerializeWithGenerics(model1);

Console.WriteLine(json1);
Console.WriteLine(json2);
Console.ReadKey();

Expected behavior

This should work exactly the same in .NET 6 and .NET Standard 2.0, given that the System.Text.Json is multi-targeting.

Actual behavior

It fails in .NET Standard 2.0 with the exception above.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6.0.100
Visual Studio 2022 17.0.1
Windows 10 20H2

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions