.Net HttpResponseMessage.Content.ReadAsAsync <Result <TResponse>> всегда получает значение NULL в значении TResponse - PullRequest
0 голосов
/ 07 мая 2018

Когда я делаю запрос с помощью Почтальона, я получаю:

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();
}

Я всегда получаю нулевое значение в свойстве Data, даже когда значения присутствуют в ответе json.

1 Ответ

0 голосов
/ 07 мая 2018

Класс результата с общедоступными сеттерами в свойствах

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; }
}

Сейчас работает.

...