【问题标题】:I got error in ASP.net core MVC and Azure iot hub我在 ASP.net 核心 MVC 和 Azure iot hub 中遇到错误
【发布时间】:2018-08-27 01:07:13
【问题描述】:

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

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

this is my iothubconnectionstring part

【问题讨论】:

  • 您提供的链接是关于 .net 框架的。不同于asp.net core,可以参考这个article
  • 您好星期天,regManager.GetDevicesAsync() 发生异常,对吧?这样就可以成功创建设备(regManager.AddDeviceAsync)了?
  • @JoeyCai 哦谢谢你我会试试的
  • @RitaHan-MSFT 谢谢,但我试过了,但无法连接到 iot hub
  • @JoeyCai 我无法在 VS2013 、 VS2015 和 VS2017 上打开该解决方案

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


【解决方案1】:

我关注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

得到设备的结果:

你可以试试看是否有帮助。

更新:

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

【讨论】:

  • 感谢您的回复。我尝试连接,但无法从 iot hub 获取设备。当我尝试直接连接互联网时,我可以从 iot hub 获取设备。所以我认为原因可能是公司网络的代理。能否请您再考虑一下,如果您知道什么,请告诉我。
  • @Sunday 似乎是known issue,它已经为.net SDK 修复了。 Here 是如何在应用程序中显式设置代理的示例。你可以试一试,如果有帮助,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
  • 2018-04-28
  • 1970-01-01
  • 2016-12-28
  • 2021-12-02
相关资源
最近更新 更多