gpt4 book ai didi

c# - 错误 'Device' 在 Controller MVC 中不包含 'GetAwaiter' 的定义

转载 作者:太空宇宙 更新时间:2023-11-03 23:09:58 25 4
gpt4 key购买 nike

在接下来的代码中

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

最佳答案

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

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

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

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

关于c# - 错误 'Device' 在 Controller MVC 中不包含 'GetAwaiter' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39646787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com