为什么当我没有使用System.IntPtr时,System.Text.Json会抛出NotSupportedException异常?

20

我有一个使用.NET Framework 4.8 / AngularJS开发的应用程序,现在想要升级到.NET 6,以便可以尝试使用更新的前端框架(例如Blazor)。升级正在进行中,但是当从Angular JS代码调用我的API方法时,我始终看到以下错误:

System.NotSupportedException: Serialization and deserialization of 'System.IntPtr' instances are not supported. Path: $.TargetSite.MethodHandle.Value.
 ---> System.NotSupportedException: Serialization and deserialization of 'System.IntPtr' instances are not supported.
   at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter`1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCoreAsObject(Utf8JsonWriter writer, Object value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteCore[TValue](JsonConverter jsonConverter, Utf8JsonWriter writer, TValue& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
   at System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
   at System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|30_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

HEADERS
=======
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Authorization: Negotiate TlRMTVNTUAADAAAAGAAYAHgAAABgAWABkAAAAAAAAABYAAAADgAOAFgAAAASABIAZgAAABAAEADwAQAAFYKI4goAYUoAAAAPV7Hpy762wOgD8P0Vp4p25WsAawBvAHoAbABlAHIASwBLAE8AWgBMAEUAUgAtAEwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAyKkYMRt+BEmGGiFVayCimwEBAAAAAAAAsRKCqZ8i2AHdnAn8ozInsAAAAAACABAAUABSAE8ATQBPAEQARQBMAAEAEgBLAEsATwBaAEwARQBSAC0ATAAEACIAYwBvAHIAcAAuAHAAcgBvAG0AbwBkAGUAbAAuAGMAbwBtAAMANgBLAEsATwBaAEwARQBSAC0ATAAuAGMAbwByAHAALgBwAHIAbwBtAG8AZABlAGwALgBjAG8AbQAFACIAYwBvAHIAcAAuAHAAcgBvAG0AbwBkAGUAbAAuAGMAbwBtAAcACACxEoKpnyLYAQYABAACAAAACAAwADAAAAAAAAAAAQAAAAAgAABZ/aEtx5BWovcqYmfEANzjrij27KQ+UdanKJzMjBJr2QoAEAAAAAAAAAAAAAAAAAAAAAAACQAcAEgAVABUAFAALwBsAG8AYwBhAGwAaABvAHMAdAAAAAAAAAAAAKxoJoGhKPec1JQ2hdsVSsQ=
Connection: keep-alive
Content-Length: 346
Content-Type: application/json;charset=UTF-8
Cookie: ai_user=r+bRv|2022-01-12T04:50:58.222Z
Host: localhost:42721
Referer: http://localhost:42721/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Origin: http://localhost:42721
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty

我很困惑这个错误。API 方法如下所示:
[HttpPost]
public IActionResult Validate([FromBody] ValidationDto dto)
{
    try
    {
        ...
    }
    catch (Exception ex)
    {
        return StatusCode((int)HttpStatusCode.InternalServerError, ex.InnermostException());
    }
}

ValidationDto 类看起来像这样:

public class ValidationDto
{
    public Guid GuidProperty { get; set; }
    public DateTime? NullableDateTimeProperty { get; set; }
    public int IntegerProperty { get; set; }
    public ValidationType EnumProperty { get; set; }
    public bool BooleanProperty { get; set; }
}
ValidationType 枚举的定义如下:
public enum ValidationType
{
    FirstType,
    SecondType,
    ThirdType,
}

我唯一指定的JSON选项如下:

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddControllers()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
        options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    });
< p >“System.Text.Json”在哪里找到无法反序列化的“IntPtr”?


添加了示例代码片段,感谢 @Serge。 - Keegan Kozler
1
回溯跟序列化有关,而非反序列化。你能分享一下你正在进行序列化的内容吗? - dbc
3
你的代码一定会抛出一个异常,你正在捕获并尝试使用 System.Text.Json 进行序列化。如果我尝试用 System.Text.Json 序列化一个“Exception”,我会得到一个错误跟踪信息,在你的问题中显示为“System.NotSupportedException: Serialization and deserialization of 'System.IntPtr' instances are not supported. Path: $.TargetSite.MethodHandle.Value.”。请参考 https://dotnetfiddle.net/RrlPQB。 - dbc
2
System.Text.Json不支持序列化异常,请参见https://github.com/dotnet/runtime/issues/43026。您可以编写转换器,但这似乎很麻烦,我无法真正推荐它。但是您在这里想要实现什么?也许[将异常作为JSON消息返回](https://dev59.com/DVkT5IYBdhLWcg3wZepe)是您想要的? - dbc
1
我认为,对于我的目的,只返回异常消息而不是异常本身应该就可以了。再次感谢您的帮助。 - Keegan Kozler
显示剩余3条评论
3个回答

22

你的代码在序列化过程中失败,而不是反序列化过程中失败,因为你捕获了一些内部异常并试图通过在此处返回它来使用System.Text.Json进行序列化:

return StatusCode((int)HttpStatusCode.InternalServerError, ex.InnermostException());

很遗憾,System.Text.Json不支持异常的序列化,请参见System.Text.Json - can't serialize exception #43026和演示fiddle https://dotnetfiddle.net/RrlPQB。它无法序列化TargetSite.MethodHandle.Value,实际上这是一个IntPtr

如果您确实需要返回所有异常数据,可以编写一个自定义的JsonConverter<Exception>。但是,更容易的方法是将必要的信息作为字符串或DTO返回,如下所示:

return StatusCode((int)HttpStatusCode.InternalServerError, ex.InnermostException().Message);

或者

return StatusCode((int)HttpStatusCode.InternalServerError, ex.InnermostException().ToString());

甚至可以使用匿名类型DTO,如下:

return StatusCode((int)HttpStatusCode.InternalServerError, new { ex.InnermostException().Message, StackTrace = ex.InnermostException().StackTrace?.ToString()} );

将异常作为JSON消息返回有额外的选项来自定义异常的返回方式。


1
接受的答案有所帮助,但我仍然需要花时间来制定最终解决方案。
public class SystemTextJsonExceptionConverter : JsonConverter<Exception>
{
    public override Exception Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, Exception value, JsonSerializerOptions options)
    {
        writer.WriteStartObject();
        writer.WriteString(nameof(Exception.Message), value.Message);

#if DEBUG
        if (value.InnerException is not null)
        {
            writer.WriteStartObject(nameof(Exception.InnerException));
            Write(writer, value.InnerException, options);
            writer.WriteEndObject();
        }

        if (value.TargetSite is not null)
        {
            writer.WriteStartObject(nameof(Exception.TargetSite));
            writer.WriteString(nameof(Exception.TargetSite.Name), value.TargetSite?.Name);
            writer.WriteString(nameof(Exception.TargetSite.DeclaringType), value.TargetSite?.DeclaringType?.FullName);
            writer.WriteEndObject();
        }

        if (value.StackTrace is not null)
        {
            writer.WriteString(nameof(Exception.StackTrace), value.StackTrace);
        }
#endif

        writer.WriteString(nameof(Type), value.GetType().ToString());
        writer.WriteEndObject();
    }
}

使用方法如下:
static void Main(string[] args)
{
    try
    {
        throw new Exception("top ex", new InvalidCastException("nested ex", new Exception("super nested ex")));
    }
    catch (Exception ex)
    {
        var settings = new JsonSerializerOptions
        {
            Converters = { new SystemTextJsonExceptionConverter() },
            WriteIndented = true
        };

        var str = JsonSerializer.Serialize(ex, options: settings);
        Console.WriteLine(str);
    }
}

适用于Asp.Net Core

services.AddControllers()
            .AddJsonOptions(o =>
            {
              //add this
                o.JsonSerializerOptions.Converters.Add(new SystemTextJsonExceptionConverter());
                o.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
            });

0

我试图返回整个搜索响应:

public async Task<IActionResult> Search()
{
    var response = await _elasticClient.SearchAsync<User>();
    return Ok(response);
}

我应该只返回文档:

public async Task<IActionResult> Search()
{
    var response = await _elasticClient.SearchAsync<User>();
    return Ok(response.Documents);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接