无法将类型'System.Threading.Tasks.Task'隐式转换为'Windows.Foundation.IAsyncAction'。存在显式转换

时间:2017-01-12 17:03:07

标签: c# multithreading azure asynchronous async-await

所以我用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));
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

DeviceClient.SendEventAsync(Message)返回Task

  

将事件发送到设备中心

public Task SendEventAsync(Message message)

将代码更改为

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

另请注意,任务继承自IAsyncResult而非IAsyncAction,如您的示例所示。

您还可以使用Task.IsCompleted Property

检查完成情况
if (Action.IsCompleted) { ...

答案 1 :(得分:0)

你的问题在这里:

deviceClient.SendEventAsync(message)

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

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

{{1}}

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

答案 2 :(得分:0)

你的问题就在这一行:

IAsyncAction Action = deviceClient.SendEventAsync(message);

SendEventAsync返回一个你期望IAsyncAction的任务。如果存在显式转换,您可以执行以下操作:

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);
相关问题