.NET 8 : deserializing of HttpContent Int payload returns null

35 views Asked by At

New to .NET 8.0. I am posting to a Rest API and expect the returned payload to be an id number. I am attempting to deserialize an HttpContent stream into a class called Result<T> where T is a payload (see code below). The int payload always deserializes as null. I have created a dictionary version of the same object to show the int Payload is coming back properly. What am I doing wrong?

Viewing both Payloads at runtime after serialization

Function to post to the API and return result:

...
try
{
    var obj = new T();
    var readTask = result.Content.ReadAsByteArrayAsync();
    byte[] bytes = null;
    bytes = readTask.Result;
    obj = readTask !=null ? JsonSerializer.Deserialize<T>(bytes) : null;
    var objAsDict = readTask !=null ? JsonSerializer.Deserialize<Dictionary<string, object>>(bytes) : null;
    return obj;
}
...
public class Result
{
    public bool Success => !ErrorDetails.Any();
    public bool Failure => !Success;
    public List<ErrorDetail> ErrorDetails { get; private set; } = new List<ErrorDetail>();
    public List<string> ExceptionDetails { get; private set; } = new List<string>();
    public List<string> Warnings { get; private set; } = new List<string>();
    ...
}

public class Result<T> : Result
{
    public T Payload { get; set; }
    ...
}

I changed to reading the result.Content using ReadAsByteArrayAsync to test incoming payload as a dictionary. I changed my payload object to be an explicit int instead of T. I verified my Result and Result<T> accessors are correct. Nothing changed with each test.

0

There are 0 answers