【问题标题】:ReadAsAsync keeps returning null WPFReadAsAsync 不断返回 null WPF
【发布时间】:2021-02-14 14:28:19
【问题描述】:

我正在尝试获取 bin 为“0001”的数据

string bin = "0001"
HttpClient client = new HttpClient();
string uri = $"https://localhost:44316/api/Bplo?bin={bin}";
HttpResponseMessage response = await client.GetAsync(uri);
 if (response.IsSuccessStatusCode)
    {
        var result = await response.Content.ReadAsAsync<BploModel>();
        return result;
    }
    else
    {
        throw new Exception(response.ReasonPhrase);
    }
  • 状态码为 200 但结果返回为空:

  • 我检查了问题是否出在端点上,但效果很好:

  • 我尝试检查我是否拼错了模型:

尝试将其添加到列表中,但仍返回 null。

【问题讨论】:

  • 你需要证明你的有效载荷是你认为的那样。使用 Fiddler,或者改为获取字符串
  • 这是一个 HTTP 响应。您确定响应包含 BploModel 对象而不是字节数组或 s 字符串吗?看起来内容无法转换为遗传参数类型。也许您应该尝试使用像 ReadAsStringAsync 这样的原始 HttpContent 方法。您需要手动反序列化任何字节数据。像类这样的数据类型是语言细节。 HTTP 规范不支持特定语言的日期类型。
  • 正如@thegeneral 所说,使用 Fiddler。但是,由于您所做的只是 GET,因此只需在浏览器中进行测试以查看服务器返回的内容
  • @BionicCode 你说得对,我的有效载荷不匹配。在我将字节数据转换为对象后,代码工作。非常感谢!!让我免于再进行 4 小时的调试,如何标记您的答案?

标签: c# wpf mvvm get caliburn.micro


【解决方案1】:

查看您的屏幕截图,您的端点似乎正在使用合理的 JSON 进行回复,您的模型似乎还不错,并且您从调用中获得了 HTTP 200。

我研究了the framework's ReadAsAsync&lt;&gt; method,并分解了它的组件,以便您可以逐步查看哪个部分不适合您:

public static async Task<T> MyReadAsAsync<T>(string url)
{
    var response = await new HttpClient().GetAsync(url);
    response.EnsureSuccessStatusCode(); // Throw up if unsuccessful
    
    /*** ReadAsAsync<> starts here ***/

    // Check the header for content type
    var contentType = response.Content.Headers.ContentType;
    // Expected "application/json"
    Debug.WriteLine($"ContentType: {contentType}");
    
    // Get available formatters in the system
    var formatters = new MediaTypeFormatterCollection();
    // Expected: more than 0
    Debug.WriteLine($"Formatters: {formatters.Count}");
    
    // Find the appropriate formatter for the content
    var formatter = formatters.FindReader(typeof(T), contentType);
    // Expected: JsonMediaTypeFormatter
    Debug.WriteLine($"Formatter: {formatter}");

    // Check the formatter
    var canRead = formatter.CanReadType(typeof(T));
    // Expected: true
    Debug.WriteLine($"CanReadType: {canRead}");

    // Check the stream
    var stream = await response.Content.ReadAsStreamAsync();
    // Expected: length of your JSON
    Debug.WriteLine($"StreamLength: {stream.Length}");
    // Expected: your JSON here
    Debug.WriteLine(System.Text.Encoding.UTF8.GetString(
        (stream as System.IO.MemoryStream)?.ToArray()));

    // Check the formatter reading and converting 
    // from the stream into an object
    var resultObj = await formatter.ReadFromStreamAsync(
        typeof(T), stream, response.Content, null);
    // Expected: an object of your type
    Debug.WriteLine($"Obj: {resultObj}");
    
    // Cast to the proper type
    var result = (T)resultObj;
    // Expected: an object of your type
    Debug.WriteLine($"Result: {result}");
    
    return result;
}

您可以通过传入您的 url 来调用该方法(另请参阅 Fiddle 以获取针对公共端点的工作测试)

var result = await MyReadAsAsync<BploModel>("https://YOUR_ENDPOINT");

【讨论】:

    【解决方案2】:

    只需在 ReasAsAsync 末尾添加 .Result()

    【讨论】:

    • 仍然报错
    • FWIW,在现代 .Net 中调用 ResultWait 很少被接受,总是有更好的方法,通常是 await
    • @TheGeneral 我需要创建一个接口并按 1 分配属性吗?
    猜你喜欢
    • 2012-05-11
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多