【问题标题】:Error when deserialize JSON microservice result in Gateway service反序列化 JSON 微服务导致网关服务出错
【发布时间】:2021-07-24 23:05:41
【问题描述】:

我有一个网关服务和一个微服务客户

网关服务调用客户微服务中的方法。

客户服务入口点如下所示:

[HttpGet("GetAll", Name = nameof(GetAll))]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<List<CustomerListVm>>> GetAll()
{
    var dtos = await _mediator.Send(new GetCustomersListQuery());

    return Ok(dtos);
}

当我从大摇大摆地调用这个方法时,结果是好的,没问题

现在我从网关服务调用同一个入口点

[HttpGet("GetAll", Name = nameof(GetAll))]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<List<CustomerDto>> GetAll()
{
    var dtos = this.customerService.GetAll();

    return Ok(dtos);
}


public async Task<List<CustomerDto>> GetAll()
{
    var response = await this.httpClient.GetAsync("url customer service Getall entry point");

    var result = await response.ReadContentAs<List<CustomerDto>>();

    return result;
}

一种将客户服务json结果的答案转换为网关服务端我的对象的扩展方法。

public static async Task<T> ReadContentAs<T>(this HttpResponseMessage response)
{
    if(!response.IsSuccessStatusCode)
        throw new ApplicationException($"Something went wrong calling the API: {response.ReasonPhrase}");

    var dataAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

    var obj = JsonSerializer.Deserialize<T>(
        dataAsString,
        new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

    return obj;
}

dataAsString 包含来自客户服务的正确结果。

[{"customerId":
        "b0788d2f-8003-43c1-92a4-edc76a7c5dde",
        "reference":"2020001",
        "lastName":"MyLastName",
        "firstName":null,
        "email":null
}]

我想把 JSON 结果放到这个对象中

public class CustomerDto
{
    public Guid CustomerId { get; set; }

    public string Reference { get; set; }

    public string LastName { get; set; }

    public string FirstName { get; set; }

    public string Email { get; set; }
}

在 startup.cs 的网关服务中,我有这个:

services.AddControllers().AddNewtonsoftJson(options =>
    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);

但是我得到一个错误,因为结果是招摇:

{
  "stateMachine": {
    "<>1__state": 0,
    "<>t__builder": {},
    "<>4__this": {}
  },
  "context": {},
  "moveNextAction": {
    "method": {
      "name": "MoveNext",
      "declaringType": "System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Collections.Generic.List`1[[Gateway.Web.Models.CustomerDto, Gateway.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Gateway.Web.Services.CustomerService+<GetAll>d__2, Gateway.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
      "reflectedType": "System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Collections.Generic.List`1[[Gateway.Web.Models.CustomerDto, Gateway.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Gateway.Web.Services.CustomerService+<GetAll>d__2, Gateway.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",

【问题讨论】:

    标签: c# json asp.net-web-api microservices api-gateway


    【解决方案1】:

    在这段代码中:

    [HttpGet("GetAll", Name = nameof(GetAll))]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public ActionResult<List<CustomerDto>> GetAll()
    {
        var dtos = this.customerService.GetAll();
    
        return Ok(dtos);
    }
    

    应该是:

    [HttpGet("GetAll", Name = nameof(GetAll))]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<ActionResult<List<CustomerDto>>> GetAll()
    {
        var dtos = await this.customerService.GetAll();
    
        return Ok(dtos);
    }
    

    你没有await它——这就是它返回序列化任务的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      相关资源
      最近更新 更多