This may seem like a duplicate of Why Async function returning System.Threading.Tasks.Task`1[System.String]? but I am doing the things suggested in the Answers there but to no avail.
I'm running a Console app:
static void Main(string[] args)
{
...
var details = CallRestMethodAsync(url, filterObj);
Console.Write(details);
Console.ReadKey();
}
public static async Task<NotificationEvents> CallRestMethodAsync(string url, FilterPager filterPager = null)
{
...
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<NotificationEvents>(result, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
}
When I put a break-point on the last line of CallRestMethodAsync
and examine result
, I find it DOES contain the object expected. And JsonConvert does not throw an error.
But the output I get is:
System.Threading.Tasks.Task`1[NotificationsHandler.NotificationEvents]
Update (4/30/2019, 11:09 CT): After accepting @Martin Brown's answer, I realized that I was (stupidly) expecting to see a stringified instance of my type output on the Console. Of course that wouldn't work. What I needed to do inside Main
was : var details = (CallRestMethodAsync(url, filterObj)).Result; // to force the Task to resolve to my type foreach (var result in details.Results) // Results being a (array) property Console.Write(details.Results[0].EventDate); // EventDate being a prop that the Console could actually output
var details = (CallRestMethodAsync(url, filterObj)).Result; // to force the Task to resolve to my type foreach (var result in details.Results) // Results being a (array) property Console.Write(details.Results[0].EventDate); // EventDate being a prop that the Console could actually output
Your CallRestMethodAsync
method returns a Task<NotificationEvents>
rather than the NotificationEvents
expected. You can set your Main
method to await
the result of CallRestMethodAsync
like this:
static async Task Main(string[] args)
{
...
var details = await CallRestMethodAsync(url, filterObj);
Console.Write(details);
Console.ReadKey();
}
If you are using an older version of .net you may not be able to create an async main method. In that case you will need to get the Result
of the task like this:
static void Main(string[] args)
{
...
var details = CallRestMethodAsync(url, filterObj);
Console.Write(details.Result);
Console.ReadKey();
}
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.