简体   繁体   中英

ReadAsAsync keeps returning null WPF

Im trying to get the data that has a bin of "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);
    }
  • Status Code is 200 but result comes back as null: 1

  • I checked if the problem was the endpoint, but it was working great: 2

  • I tried checking if I misspelled the model: 3

tried adding it on a List but still returns null.

Looking at your screenshot, it seems your endpoint is replying with a reasonable JSON, your Model seems okay, and you are getting a HTTP 200 from the call.

I dug into the framework's ReadAsAsync<> method , and broken down its component so you can step through and see which part is failing for you:

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

You can call the method by passing in your url (also see Fiddle for a working test against a public endpoint)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM