错误& # 39;设备# 39;不包含'getawait '在控制器MVC中

本文关键字:控制器 MVC 设备 错误 包含 getawait | 更新日期: 2023-09-27 18:08:17

下一段代码

 [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);
    }

我有下一个错误:

错误CS1061 'Device'不包含'GetAwaiter'的定义,并且没有找到接受'Device'类型的第一个参数的扩展方法'GetAwaiter'(您是否缺少using指令或程序集引用?)

在"设备"中有:

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; }
    }
}

错误& # 39;设备# 39;不包含'getawait '在控制器MVC中

您必须等待异步方法返回的任务,而不是task. result。当使用await时,返回表达式的值将已经是任务。结果属性。

因此,在异步调用之后删除。result,它应该可以正常工作。

try
{
    device =  await registryManager.AddDeviceAsync(new Device(dispositivo.Nombre));
 }
 catch (DeviceAlreadyExistsException)
 {
    device = await registryManager.GetDeviceAsync(dispositivo.Nombre);
 }