2016-09-22 56 views
1

在接下來的代碼爲「GetAwaiter」的定義錯誤「設備」不包含在controlller MVC

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

我有一個錯誤:

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?)

在「設備」有:

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

}

回答

3

你甲肝e等待異步方法返回的任務,而不是任務。結果。當使用await時,返回表達式的值將已經是task.Result屬性。

因此,在異步調用後刪除.Result,它應該可以正常工作。對於

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

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

要點Async-Await

  1. 只能await一個Task or Task<T>
  2. await是強制性的製作方法Async,否則它就像一個同步方法

  3. await自動解包價值,因此:

    設備=等待<Task<Device> from Async call>

是完全正常的,將填補該設備對象的值,你不需要在Task

很重要的一點Result呼叫使用類似Task.Result或如果您打算刪除await並堅持使用Result來填充設備對象,則會在運行時導致Deadlock在運行時由於阻止Synchronization context而導致Task.Wait, n運行時問題,請檢查here