【问题标题】:Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'Windows.Foundation.IAsyncAction'. An explicit conversion exists无法将类型“System.Threading.Tasks.Task”隐式转换为“Windows.Foundation.IAsyncAction”。存在显式转换
【发布时间】:2017-01-12 17:03:07
【问题描述】:

所以我正在用 C# 编写这个软件,我收到了这个错误消息。对于我的生活,我无法弄清楚,无论我做什么,搜索都不会带来任何有用的东西。

代码本身:

private async Task LoginIoTHub()
{
    TypeJson command_json = new TypeJson();

    if (NowNetSta == false) return;
    if (IoTHubConnectFlag == false) return;

    if (TeamName == null)
    {
        Debug.WriteLine("Please don't let your team name be void when you call methods 'new IoTHubLocalService(xx,xx,xx)'");
        await Task.Delay(TimeSpan.FromSeconds(4));
    }
    else
    {
        if (LoginF == false)
        {
            try
            {
                Debug.WriteLine("Start to login\r\n");

                LoginJson login_msg = new LoginJson();
                login_msg.DeviceID = BoardID;
                login_msg.name = TeamName;

                var messageString = JsonConvert.SerializeObject(login_msg);

                command_json.Type = 1;
                command_json.Command = messageString;
                messageString = JsonConvert.SerializeObject(command_json);

                var message = new Message(Encoding.ASCII.GetBytes(messageString));
                IAsyncAction Action = deviceClient.SendEventAsync(message);

                await Action;

                if (Action.Status == AsyncStatus.Completed)
                {
                    Debug.WriteLine("Login success");
                    LoginF = true;
                }
                else
                {
                    Debug.WriteLine("Login failed");
                    await Task.Delay(TimeSpan.FromSeconds(10));
                }
            }

            catch (Exception ex)
            {
                Debug.WriteLine("Login to IoT Hub failed, please check your team name and internet connection: " + ex.Message);
                await Task.Delay(TimeSpan.FromSeconds(10));
            }
        }
    }
}

【问题讨论】:

  • 错误是哪一行。是IAsyncAction Action = deviceClient.SendEventAsync(message);我猜这是你的问题。

标签: c# multithreading azure asynchronous async-await


【解决方案1】:

DeviceClient.SendEventAsync(Message) 返回一个Task

向设备集线器发送事件

public Task SendEventAsync(Message message)

把代码改成

var Action = deviceClient.SendEventAsync(message);
await Action;

另请注意,Task 继承自 IAsyncResult,而不是像您的示例中的 IAsyncAction

您还可以使用Task.IsCompleted Property检查完成情况

if (Action.IsCompleted) { ...

【讨论】:

  • 这部分有效,但 if 语句再次中断。 “Action.Status”失败(只读)或双“==”失败。
  • 是的,你做到了。我很抱歉。这是漫长的一天。
【解决方案2】:

你的问题在这里:

IAsyncAction Action = deviceClient.SendEventAsync(message);

异步方法调用返回任务。不是 IAsyncActions。

假设您的函数调用deviceClient.SendEventAsync(message) 的返回值是Task<IAsyncAction>,您应该将您的代码更新为:

IAsyncAction Action = await deviceClient.SendEventAsync(message);

参考:https://msdn.microsoft.com/en-us/library/mt674882.aspx

【讨论】:

  • 我试过了,信不信由你。但它会引发另一个错误:“无法将类型 'void' 隐式转换为 'Windows.Foundation.IASyncAction'。
【解决方案3】:

你的问题是这一行:

IAsyncAction Action = deviceClient.SendEventAsync(message);

无论您期望 IAsyncAction,SendEventAsync 返回一个 Task。如果存在显式转换,您可以执行以下操作:

IAsyncAction Action = deviceClient.SendEventAsync(message) as IAsyncResult;

但我认为 SendEventAsync 在内部返回 Task<IAsyncAction> 这意味着您必须等待它:

IAsyncAction Action = await deviceClient.SendEventAsync(message);

根据您的评论:

我试过了,信不信由你。但是它会引发另一个错误: “不能将类型‘void’隐式转换为 'Windows.Foundation.IASyncAction'。

我猜你只是没有获得 IAsyncAction。所以你必须这样做:

await deviceClient.SendEventAsync(message);

【讨论】:

  • 我自己就是这么想的!现在可以了,谢谢大家的帮助!
  • @msammels 欢迎您。请接受适合您需求的答案,如果答案对您有帮助,请点赞。
  • 刚刚遇到另一个问题。如果我使用“await deviceClient”解决方案,下面的 if 语句就会中断。还有什么想法吗?
  • @msammels 你应该提出一个新问题或更新这个问题,并附上实际代码和错误描述
猜你喜欢
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多