.Net HttpResponseMessage.Content.ReadAsAsync<Result<TResponse>> 始终在 TResponse 值中获取空值
.Net HttpResponseMessage.Content.ReadAsAsync<Result<TResponse>> getting null always in the TResponse value
当我使用 Postman 发出请求时,我得到:
这就是我所期待的:
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();
}
我总是在 属性 数据中得到空值,即使值在 json 响应中也是如此。
结果 class 在属性中有 public 个设置器
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; }
}
现在正在运行。
当我使用 Postman 发出请求时,我得到:
这就是我所期待的:
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();
}
我总是在 属性 数据中得到空值,即使值在 json 响应中也是如此。
结果 class 在属性中有 public 个设置器
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; }
}
现在正在运行。