错误'设备'不包含' GetAwaiter'的定义在控制器MVC中

时间:2016-09-22 18:50:39

标签: c# async-await

在下一个代码中

 [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&#39;设备&#39;不包含&#39; GetAwaiter&#39;的定义没有扩展方法&#39; GetAwaiter&#39;接受类型&#39; Device&#39;的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

In&#34; Device&#34;有:

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 个答案:

答案 0 :(得分:3)

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

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

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

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