diff --git a/src/Directory.Build.props b/src/Directory.Build.props
index f55eb82..ffe21d8 100644
--- a/src/Directory.Build.props
+++ b/src/Directory.Build.props
@@ -6,7 +6,7 @@
enable
1.0.0
- preview.2
+ preview.3
diff --git a/src/Ling.RemoteServices.AspNetCore/RemoteServiceServerRuntime.cs b/src/Ling.RemoteServices.AspNetCore/RemoteServiceServerRuntime.cs
index 5742485..91c5c6e 100644
--- a/src/Ling.RemoteServices.AspNetCore/RemoteServiceServerRuntime.cs
+++ b/src/Ling.RemoteServices.AspNetCore/RemoteServiceServerRuntime.cs
@@ -1,4 +1,5 @@
using System.ComponentModel;
+using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Ling.RemoteServices.Exceptions;
@@ -106,6 +107,11 @@ public static JsonTypeInfo GetJsonTypeInfo(JsonSerializerOptions options)
{
ArgumentNullException.ThrowIfNull(options);
+ if (options.TypeInfoResolver is null && JsonSerializer.IsReflectionEnabledByDefault)
+ {
+ options.TypeInfoResolver = CreateReflectionJsonTypeInfoResolver();
+ }
+
try
{
if (options.GetTypeInfo(typeof(T)) is JsonTypeInfo typeInfo)
@@ -146,6 +152,16 @@ private static InvalidOperationException CreateMissingJsonMetadataException(
+ "with ConfigureHttpJsonOptions when using Native AOT.",
innerException);
}
+
+ [UnconditionalSuppressMessage(
+ "Trimming",
+ "IL2026",
+ Justification = "This fallback is reachable only when JSON reflection is enabled.")]
+ [UnconditionalSuppressMessage(
+ "AOT",
+ "IL3050",
+ Justification = "This fallback is reachable only when JSON reflection is enabled.")]
+ private static DefaultJsonTypeInfoResolver CreateReflectionJsonTypeInfoResolver() => new();
}
///
diff --git a/src/Ling.RemoteServices.Client/RemoteServiceClientRuntime.cs b/src/Ling.RemoteServices.Client/RemoteServiceClientRuntime.cs
index 80688d6..872a300 100644
--- a/src/Ling.RemoteServices.Client/RemoteServiceClientRuntime.cs
+++ b/src/Ling.RemoteServices.Client/RemoteServiceClientRuntime.cs
@@ -1,5 +1,6 @@
using System.Collections;
using System.ComponentModel;
+using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net.Http.Json;
using System.Text;
@@ -116,6 +117,11 @@ public static JsonTypeInfo GetJsonTypeInfo(JsonSerializerOptions options)
{
ArgumentNullException.ThrowIfNull(options);
+ if (options.TypeInfoResolver is null && JsonSerializer.IsReflectionEnabledByDefault)
+ {
+ options.TypeInfoResolver = CreateReflectionJsonTypeInfoResolver();
+ }
+
try
{
if (options.GetTypeInfo(typeof(T)) is JsonTypeInfo typeInfo)
@@ -195,4 +201,14 @@ private static InvalidOperationException CreateMissingJsonMetadataException(
+ "with RemoteServiceClientOptions.AddJsonSerializerContext when using Native AOT.",
innerException);
}
+
+ [UnconditionalSuppressMessage(
+ "Trimming",
+ "IL2026",
+ Justification = "This fallback is reachable only when JSON reflection is enabled.")]
+ [UnconditionalSuppressMessage(
+ "AOT",
+ "IL3050",
+ Justification = "This fallback is reachable only when JSON reflection is enabled.")]
+ private static DefaultJsonTypeInfoResolver CreateReflectionJsonTypeInfoResolver() => new();
}
diff --git a/tests/Ling.RemoteServices.AspNetCore.Tests/RemoteServiceServerRuntimeTests.cs b/tests/Ling.RemoteServices.AspNetCore.Tests/RemoteServiceServerRuntimeTests.cs
new file mode 100644
index 0000000..9937b95
--- /dev/null
+++ b/tests/Ling.RemoteServices.AspNetCore.Tests/RemoteServiceServerRuntimeTests.cs
@@ -0,0 +1,21 @@
+using System.Text.Json;
+using System.Text.Json.Serialization.Metadata;
+using Ling.RemoteServices.AspNetCore;
+
+namespace Ling.RemoteServices.AspNetCore.Tests;
+
+public class RemoteServiceServerRuntimeTests
+{
+ [Fact]
+ public void GetJsonTypeInfo_uses_reflection_metadata_by_default_on_jit_hosts()
+ {
+ var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
+
+ var typeInfo = RemoteServiceServerRuntime.GetJsonTypeInfo(options);
+
+ Assert.Equal(typeof(TestPayload), typeInfo.Type);
+ Assert.IsType(options.TypeInfoResolver);
+ }
+
+ private sealed record TestPayload(string Value);
+}
diff --git a/tests/Ling.RemoteServices.Client.Tests/RemoteServiceRuntimeTests.cs b/tests/Ling.RemoteServices.Client.Tests/RemoteServiceRuntimeTests.cs
index 5aae8ba..5033ddd 100644
--- a/tests/Ling.RemoteServices.Client.Tests/RemoteServiceRuntimeTests.cs
+++ b/tests/Ling.RemoteServices.Client.Tests/RemoteServiceRuntimeTests.cs
@@ -4,6 +4,19 @@ namespace Ling.RemoteServices.Client.Tests;
public class RemoteServiceRuntimeTests
{
+ [Fact]
+ public void GetJsonTypeInfo_uses_reflection_metadata_by_default_on_jit_hosts()
+ {
+ var options = new RemoteServiceClientOptions();
+
+ var typeInfo = RemoteServiceClientRuntime.GetJsonTypeInfo(
+ options.JsonSerializerOptions);
+
+ Assert.Equal(typeof(ClientTestPayload), typeInfo.Type);
+ Assert.IsType(
+ options.JsonSerializerOptions.TypeInfoResolver);
+ }
+
[Fact]
public void GetJsonTypeInfo_uses_registered_source_generated_context()
{