So I'm coding this software in C# and I get hit with this error message. For the life of me, I cannot figure it out and searching is not bringing up anything useful, no matter what I do.
The code itself:
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));
}
}
}
}
DeviceClient.SendEventAsync(Message) returns a Task
Sends an event to device hub
public Task SendEventAsync(Message message)
change code to
var Action = deviceClient.SendEventAsync(message);
await Action;
Also note that Task inherits from IAsyncResult
and not IAsyncAction
like in your example.
You can also check completion using Task.IsCompleted Property
if (Action.IsCompleted) { ...
Your problem is here:
IAsyncAction Action = deviceClient.SendEventAsync(message);
Async method calls return Tasks. Not IAsyncActions.
Assuming the return value of your function call deviceClient.SendEventAsync(message)
is a Task<IAsyncAction>
you should updated your code to this:
IAsyncAction Action = await deviceClient.SendEventAsync(message);
Reference: https://msdn.microsoft.com/en-us/library/mt674882.aspx
Your problem is this line:
IAsyncAction Action = deviceClient.SendEventAsync(message);
SendEventAsync return a Task wether you expect a IAsyncAction. If an explicit conversion exists you can do the following:
IAsyncAction Action = deviceClient.SendEventAsync(message) as IAsyncResult;
But I think SendEventAsync returns Task<IAsyncAction>
inside which means you have to await it:
IAsyncAction Action = await deviceClient.SendEventAsync(message);
Based on your comment:
I tried this, believe it or not. It throws another error though: "Cannot implicitly convert type 'void' to 'Windows.Foundation.IASyncAction'.
I would guess that you just don't get a IAsyncAction. So you have to do it this way:
await deviceClient.SendEventAsync(message);
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.