c# - 我在 ASP.net core MVC 和 Azure iot hub 中遇到错误

标签 c# azure asp.net-core azure-iot-hub

我是新手 现在我尝试使用 ASp.net core 通过本教程在 Azure iot hub 中获取一些内容:https://ztirom.at/2016/03/frist-steps-azure-iot-hub/ 但是当我遇到一些错误

enter image description here

发生了什么错误吗? 我该如何修复它或从物联网中心获取一些数据的另一种方法。 谢谢大家

this is my iothubconnectionstring part

最佳答案

我关注this tutorial创建 ASP.NET Core MVC 应用程序。从 NuGet 安装 Microsoft.Azure.Devices(版本 1.17.0)。在 Controller 中调用regManager.GetDevicesAsync(),成功获取结果。

Controller 中的ManageDevices.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Common.Exceptions;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApplication1.Controllers
{
    public class ManageDevices : Controller
    {
        public static string IOTHUBSTRING = "HostName=[IOT HUB NAME].azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=[KEY]";


        // GET: /<controller>/
        public string Index()
        {
            return "This is my default action...";
        }

        public IActionResult GetDevices()
        {
            GetDevicesAsync();
            return View();
        }

        public async static Task<IEnumerable> GetDevicesAsync(int deviceCount = 8)
        {

            var regManager = RegistryManager.CreateFromConnectionString(IOTHUBSTRING);

            var devices = await regManager.GetDevicesAsync(deviceCount);

            foreach (var device in devices)
            {
                string deviceId = device.Id;
                DeviceStatus status = device.Status;
            }

            return devices;
        }
    }
}

您可以使用以下 URI 来执行 GetDevices 方法:

https://localhost:44387/ManageDevices/GetDevices

enter image description here

获取设备结果:

enter image description here

您可以尝试一下是否有帮助。

更新:

您可以访问以下设备数据:

enter image description here

关于c# - 我在 ASP.net core MVC 和 Azure iot hub 中遇到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52031439/

相关文章:

azure - Microsoft Bot Framework - 响应时间非常长

entity-framework - EF Core OnDelete 限制添加附加列

c# - 如何从Button Click事件获取返回值?

c++ - 5_4-2016q3\bin\arm-none-eabi-g++ 未找到 - Windows 10

c# - 防止显示一种形式的多个实例

azure - 如何使用javascript UDF将Azure流分析中的 'dictionary-like'结构转换为多维数组?

visual-studio-2015 - 在使用 ASP.NET Core 进行集成测试期间使用多个环境

asp.net-core - 如何用我自己的 IoC 替换内置 IoC?

c# - 在弹出窗口关闭时刷新页面?

c# - 从 Excel 互操作读取并将不同的值写入另一个工作表