简体   繁体   English

System.Threading.Tasks.Task`1 [ <myType> ]被退回

[英]System.Threading.Tasks.Task`1[<myType>] being returned

This may seem like a duplicate of Why Async function returning System.Threading.Tasks.Task`1[System.String]? 这似乎是为什么异步函数返回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. 当我在CallRestMethodAsync的最后一行放置一个断点并检查result ,我发现它确实包含预期的对象。 And JsonConvert does not throw an error. 而且,JsonConvert不会引发错误。

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. 更新(4/30/2019,11:09 CT):在接受@Martin Brown的回答后,我意识到我(愚蠢地)期望在控制台上看到我的类型输出的字符串化实例。 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 我需要在Main内部做的是: 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. 您的CallRestMethodAsync方法返回Task<NotificationEvents>而不是预期的NotificationEvents You can set your Main method to await the result of CallRestMethodAsync like this: 您可以将Main方法设置为await CallRestMethodAsync的结果,如下所示:

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. 如果使用的是.net的旧版本,则可能无法创建异步main方法。 In that case you will need to get the Result of the task like this: 在这种情况下,您将需要获得以下任务的Result

static void Main(string[] args)
{
    ...

    var details = CallRestMethodAsync(url, filterObj);
    Console.Write(details.Result);
    Console.ReadKey();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将 System.Threading.Tasks.Task... 转换为 System.Threading.Tasks.Task... 尽管看似相同的类型 - Cannot convert System.Threading.Tasks.Task… to System.Threading.Tasks.Task… despite seemingly being same type 找不到 System.Threading.Tasks.Task 方法 - System.Threading.Tasks.Task Method Not Found 找不到方法:'System.Threading.Tasks.Task`1<!--!0--> - Method not found: 'System.Threading.Tasks.Task`1<!!0> 无法将类型&#39;void&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task' 运行System.Threading.Tasks.Task时出现异常 - Exception while running System.Threading.Tasks.Task 无法隐式转换类型&#39;System.Threading.Tasks.Task - Cannot implicitly convert type 'System.Threading.Tasks.Task 如何获得 System.Threading.Tasks.Task 已完成的通知 - How to get notification that a System.Threading.Tasks.Task has completed System.Threading.Tasks.Task在循环中执行奇怪的行为 - System.Threading.Tasks.Task executed in a loop strange behavior 有没有办法发信号通知System.Threading.Tasks.Task? - Is there a way to signal a System.Threading.Tasks.Task complete? 跟踪已完成/正在运行的System.Threading.Tasks.Task对象 - Tracking completed/running System.Threading.Tasks.Task objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM