使用新的 "System.Text.Json" class(Asp.net 核心 3.0 预览版 8)的 System.Data.DataTable 序列化异常

Exception in serialization of System.Data.DataTable using the new "System.Text.Json" class (Asp.net core 3.0 preview 8)

我正在 asp.net 核心 3.0 预览版 8 中写一个休息 api 我正在尝试使用新的 "System.Text.Json" class 序列化一个 System.Data.DataTable ,但在 Serialize 方法中我收到异常:

The collection type 'System.Data.DataRelationCollection' on 'System.Data.DataTable.ChildRelations' is not supported.

相同的序列化使用 newtonsoft json 序列化程序效果很好。

重现问题的示例代码:

var dt = new System.Data.DataTable("test");
dt.Columns.Add("Column1");
var ser=System.Text.Json.JsonSerializer.Serialize(dt);

详细异常:

System.NotSupportedException HResult=0x80131515 Message=The collection type 'System.Data.DataRelationCollection' on 'System.Data.DataTable.ChildRelations' is not supported. Source=System.Text.Json StackTrace: at System.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options) at System.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, Type implementedPropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonSerializerOptions options) at System.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options) at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options) at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType) at System.Text.Json.WriteStackFrame.Initialize(Type type, JsonSerializerOptions options) at System.Text.Json.JsonSerializer.WriteCore(Utf8JsonWriter writer, PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options) at System.Text.Json.JsonSerializer.WriteCore(PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options) at System.Text.Json.JsonSerializer.WriteCoreString(Object value, Type type, JsonSerializerOptions options) at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options) at ErrorJsonMIcrosoftDataTable.Controllers.WeatherForecastController.Get() in G:\testnet\ErrorJsonMIcrosoftDataTable\ErrorJsonMIcrosoftDataTable\Controllers\WeatherForecastController.cs:line 31 at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<g__Logged|12_1>d.MoveNext()

你能帮忙吗?

谢谢。

简答:用System.Text.Json做不到,至少目前是这样。

如果您想使用 ASP.NET Core 3.0 和今天可用的内容序列化 System.Data.DataTable,请继续阅读我的 post 的其余部分以获得解决方法。

解决方法: 首先,你应该检查来自MS的"Migrate from ASP.NET Core 2.2 to 3.0"文档的"Json.NET support"。

解决方案有 2 个步骤:

  1. 添加对 "Microsoft.AspNetCore.Mvc.NewtonsoftJson"

  2. 的包引用
  3. 在 "services.AddMvc()" 之后添加这一行“.AddNewtonsoftJson()” 以下是 Startup.cs 更改前后的示例:

之前:

services
    .AddMvc(options =>
    {
        options.EnableEndpointRouting = false;
    })
    .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.IgnoreNullValues = true;
        options.JsonSerializerOptions.WriteIndented = true;
    });

之后:

services
    .AddMvc(options =>
    {
        options.EnableEndpointRouting = false;
    })
    .AddNewtonsoftJson()
    .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.IgnoreNullValues = true;
        options.JsonSerializerOptions.WriteIndented = true;
    });