代码之家  ›  专栏  ›  技术社区  ›  Lizzael Villar Cruz

.Net HttpResponseMessage。所容纳之物ReadAsAsync<结果<响应>>始终在响应值中获取null

  •  -3
  • Lizzael Villar Cruz  · 技术社区  · 6 年前

    当我使用邮递员进行请求时,我得到:

    enter image description here

    这就是我所期待的:

    public class Result<TResultType>
    {
        public int? ErrorId { get; protected set; } // Todo: Use it!!!
        public bool Success { get; protected set; }
        public ResponseErrorType ErrorType { get; protected set; }
        public string Message { get; protected set; }
        public TResultType Data { get; protected set; }
    }
    

    在这种情况下,TResultType为:

    public class SettingsResponse : BaseResponse
    {
        public string LocationCode { get; set; }
        public string Ip { get; set; }
        public int Port { get; set; }
        public string ApplicationPrinterName { get; set; }
        public string MerchantNumber { get; set; }
        public string MessageControl { get; set; }
        public string PRIN { get; set; }
        public string SoftwareName { get; set; }
        public string SoftwareVersion { get; set; }
        public string SystemNumber { get; set; }
    }
    

    我是这样做的:

    using (HttpClient client = GetHttpClient())
    {
        var responseTask = client.GetAsync($"{url}/{paramsStr}");
        if (await Task.WhenAny(responseTask, Task.Delay(TimeoutSeconds * 1000)) == responseTask)
        {
            var response = await responseTask;
            return response.IsSuccessStatusCode
                ? await response.Content.ReadAsAsync<Result<TResponse>>()
                : default(Result<TResponse>);
        }
    
        return Result<TResponse>.FromTimeout();
    }
    

    我总是在属性数据中得到null,即使json响应中有值。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Lizzael Villar Cruz    6 年前

    属性中具有公共setter的结果类

    public class Result<TResultType>
    {
        public int? ErrorId { get; set; } // Todo: Use it!!!
        public bool Success { get; set; }
        public ResponseErrorType ErrorType { get; set; }
        public string Message { get; set; }
        public TResultType Data { get; set; }
    }
    

    它现在正在工作。