我在ASP.net核心MVC和Azure iot中心出错

问题描述 投票:1回答:1

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

enter image description here

出了什么问题?我该如何解决它或从iot hub获取一些数据的另一种方法。谢谢大家

this is my iothubconnectionstring part

c# azure asp.net-core azure-iot-hub
1个回答
0
投票

我按照this tutorial创建一个ASP.NET Core MVC应用程序。从NuGet安装Microsoft.Azure.Devices(版本1.17.0)。调用控制器中的regManager.GetDevicesAsync()并成功获取结果。

控制器中的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

© www.soinside.com 2019 - 2024. All rights reserved.