我们有一个API,它简单地将传入的JSON文档发布到消息总线,并为每个文档分配了一个GUID。我们正在从.Net Core 2.2升级到3.1,并打算使用新的System.Text.Json
库替换NewtonSoft。
我们反序列化传入的文档,将GUID分配给其中一个字段,然后重新序列化再发送到消息总线。不幸的是,重新序列化失败,抛出异常由于对象当前的状态无效,因此操作无效
。
这里有一个控制器展示了这个问题:
using System;
using System.Net;
using Project.Models;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Text;
using System.Text.Json;
namespace Project.Controllers
{
[Route("api/test")]
public class TestController : Controller
{
private const string JSONAPIMIMETYPE = "application/vnd.api+json";
public TestController()
{
}
[HttpPost("{eventType}")]
public async System.Threading.Tasks.Task<IActionResult> ProcessEventAsync([FromRoute] string eventType)
{
try
{
JsonApiMessage payload;
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8)) {
string payloadString = await reader.ReadToEndAsync();
try {
payload = JsonSerializer.Deserialize<JsonApiMessage>(payloadString);
}
catch (Exception ex) {
return StatusCode((int)HttpStatusCode.BadRequest);
}
}
if ( ! Request.ContentType.Contains(JSONAPIMIMETYPE) )
{
return StatusCode((int)HttpStatusCode.UnsupportedMediaType);
}
Guid messageID = Guid.NewGuid();
payload.Data.Id = messageID.ToString();
// we would send the message here but for this test, just reserialise it
string reserialisedPayload = JsonSerializer.Serialize(payload);
Request.HttpContext.Response.ContentType = JSONAPIMIMETYPE;
return Accepted(payload);
}
catch (Exception ex)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
}
}
}
JsonApiMessage对象的定义如下:
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Project.Models
{
public class JsonApiMessage
{
[JsonPropertyName("data")]
public JsonApiData Data { get; set; }
[JsonPropertyName("included")]
public JsonApiData[] Included { get; set; }
}
public class JsonApiData
{
[JsonPropertyName("type")]
public string Type { get; set; }
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("attributes")]
public JsonElement Attributes { get; set; }
[JsonPropertyName("meta")]
public JsonElement Meta { get; set; }
[JsonPropertyName("relationships")]
public JsonElement Relationships { get; set; }
}
}
一个典型的调用看起来像这样:
POST http://localhost:5000/api/test/event
Content-Type: application/vnd.api+json; charset=UTF-8
{
"data": {
"type": "test",
"attributes": {
"source": "postman",
"instance": "jg",
"level": "INFO",
"message": "If this comes back with an ID, the API is probably working"
}
}
}
当我在Visual Studio的断点处检查payload
的内容时,顶层看起来没问题,但是JsonElement
的部分看起来不透明,所以我不知道它们是否已被正确解析。它们的结构可能会有所变化,因此我们只关心它们是有效的JSON。在旧的NewtonSoft版本中,它们是JObject
。
在GUID添加后,在检查点检查payload
对象时,它会出现,但我怀疑问题与对象中的其他元素是只读的或类似的东西有关。
JsonSerializer.Deserialize<JsonApiMessage>
的调用。我怀疑错误与JSON无关。你应该发布完整的异常,包括堆栈跟踪,而不仅仅是消息。堆栈跟踪显示了异常抛出的位置以及导致它的调用。你可以通过Exception.ToString()
轻松获取它,或者在调试器的异常弹出窗口中单击Copy Exception Details
来获取它。 - Panagiotis KanavosJsonApiMessage
作为操作参数传递呢?这样你就可以避免绕过流水线和System.Text.Json反序列化的优化,并且不会失去任何好处。通过使用StreamReader并将数据缓存到payloadString
中,你将获得完整的分配。 - Panagiotis KanavosUndefined
元数据和关系值,这是由于缺少属性造成的。 - Panagiotis Kanavos