Skip to main content

Fixed: "Reflection-based serialization has been disabled for this application" with dotnet run

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

JsonSerializer.Serialize is about as ordinary as C# gets, so Reflection-based serialization has been disabled for this application was a surprise, in a one-file program with nothing else in it. File-based apps turn this off by default. There are two ways to deal with that.

What I Was Doing​

I wrote a one-file C# program with the .NET 10 SDK, to print an object as JSON:

app.cs
using System.Text.Json;

Console.WriteLine(JsonSerializer.Serialize(new { Id = 1, Name = "Cable" }));
Terminal
dotnet run app.cs

How to Run a Single C# File with dotnet run explains this way of running C#.

What I Expected​

One line of JSON.

What I Got​

Output
/Users/you/projects/hello/app.cs(3,19): warning IL2026: Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
/Users/you/projects/hello/app.cs(3,19): warning IL3050: Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.
Unhandled exception. System.InvalidOperationException: Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property.

dotnet run app.cs printing warnings IL2026 and IL3050 about JsonSerializer.Serialize, then Unhandled exception. System.InvalidOperationException: Reflection-based serialization has been disabled for this application.

What I Checked​

Whether the same code works in a project. It does: System.Text.Json uses reflection by default. So something was different about a one-file program.

What a one-file program turns on. Microsoft's file-based apps page says: "File-based apps enable native AOT publishing by default, producing optimized, self-contained executables." I looked at the settings of a build like this one. On my Mac they're in app.runtimeconfig.json, under ~/Library/Application Support/dotnet/runfile/:

app.runtimeconfig.json
"System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault": false

What Microsoft says about this setting. Its source generation page explains why it exists: "Because System.Text.Json uses reflection by default, calling a basic serialization method can break Native AOT apps, which doesn't support all required reflection APIs."

Why It Happens​

Native AOT doesn't support all the reflection System.Text.Json needs, so an app set up for it has reflection off. A file-based app is set up for it by default, even when you only run it with dotnet run. So the default serializer has nothing to work with, and it says so.

The Fix​

If you don't need native AOT, turn it off, on the first line of the file:

app.cs
#:property PublishAot=false

using System.Text.Json;

Console.WriteLine(JsonSerializer.Serialize(new { Id = 1, Name = "Cable" }));

If you do want native AOT, use the source generator, as the error suggests. It needs a named type, because it can't generate code for an anonymous one:

app.cs
using System.Text.Json;
using System.Text.Json.Serialization;

Console.WriteLine(JsonSerializer.Serialize(new Product(1, "Cable"), AppJsonContext.Default.Product));

record Product(int Id, string Name);

[JsonSerializable(typeof(Product))]
partial class AppJsonContext : JsonSerializerContext;

After the Fix​

Both give the same output, and the warnings have gone:

Terminal
dotnet run app.cs
Output
{"Id":1,"Name":"Cable"}

With the fix in the file, dotnet run app.cs printing the JSON with Id 1 and Name Cable, with no warnings.

Common Mistakes​

MistakeWhat happensDo this instead
Putting the line below a usingCS9297Put it first
Using the source generator with an anonymous typeIt needs a type it can nameDeclare a record
Ignoring warnings IL2026 and IL3050They told me about this before it failedRead them

Troubleshooting​

SymptomLikely causeFix
Model building is not supported when publishing with NativeAOTThe same setting, in EF CoreThe fix
The same error in a normal projectPublishAot or JsonSerializerIsReflectionEnabledByDefault in the .csprojCheck the project file

Conclusion​

File-based apps have System.Text.Json's reflection off by default. Add #:property PublishAot=false as the first line, or use a source generator if you need native AOT.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.