1
votes

In the next code

 [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "DispositivoID,Nombre,ClaveDispositivo,Activo,EmpresaID")] Modelo.Dispositivo dispositivo)
    {
        if (ModelState.IsValid)
        {
            //string deviceId = "minwinpc";
            Device device;
            try
            {
                device =  await registryManager.AddDeviceAsync(new Device(dispositivo.Nombre)).Result;

            }
            catch (DeviceAlreadyExistsException)
            {
                device = await registryManager.GetDeviceAsync(dispositivo.Nombre).Result;
            }

            dispositivo.ClaveDispositivo = device.Authentication.SymmetricKey.PrimaryKey;
            db.Dispositivos.Add(dispositivo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.EmpresaID = new SelectList(db.Empresas, "EmpresaID", "Nombre", dispositivo.EmpresaID);
        return View(dispositivo);
    }

I have the next error:

Error CS1061 'Device' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'Device' could be found (are you missing a using directive or an assembly reference?)

In "Device" there are:

using System;
using Microsoft.Azure.Devices.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Microsoft.Azure.Devices
{
    public class Device : IETagHolder
    {
        public Device();
        public Device(string id);

        [JsonProperty(PropertyName = "authentication")]
        public AuthenticationMechanism Authentication { get; set; }
        [JsonProperty(PropertyName = "cloudToDeviceMessageCount")]
        public int CloudToDeviceMessageCount { get; }
        [JsonConverter(typeof(StringEnumConverter))]
        [JsonProperty(PropertyName = "connectionState")]
        public DeviceConnectionState ConnectionState { get; }
        [JsonProperty(PropertyName = "connectionStateUpdatedTime")]
        public DateTime ConnectionStateUpdatedTime { get; }
        [JsonProperty(PropertyName = "etag")]
        public string ETag { get; set; }
        [JsonProperty(PropertyName = "generationId")]
        public string GenerationId { get; }
        [JsonProperty(PropertyName = "deviceId")]
        public string Id { get; }
        [JsonProperty(PropertyName = "lastActivityTime")]
        public DateTime LastActivityTime { get; }
        [JsonConverter(typeof(StringEnumConverter))]
        [JsonProperty(PropertyName = "status")]
        public DeviceStatus Status { get; set; }
        [JsonProperty(PropertyName = "statusReason")]
        public string StatusReason { get; set; }
        [JsonProperty(PropertyName = "statusUpdatedTime")]
        public DateTime StatusUpdatedTime { get; }
    }
}
1

1 Answers

3
votes

You have to await the task that the async method returns, not the task.Result. When using await, the value of the returning expression will be already the task.Result property.

So remove the .Result after the async calls and it should work correctly.

try
{
    device =  await registryManager.AddDeviceAsync(new Device(dispositivo.Nombre));

 }
 catch (DeviceAlreadyExistsException)
 {
    device = await registryManager.GetDeviceAsync(dispositivo.Nombre);
 }