I got error in ASP.net core MVC and Azure iot hub
I got error in ASP.net core MVC and Azure iot hub
I`m Novice
Now I try to use ASp.net core to get some in Azure iot hub with this tutorial:https://ztirom.at/2016/03/frist-steps-azure-iot-hub/
but when I got a some error
What is something wrong happen?
how can I fix it or Another way to get some data from iot hub.
thank you everyone
this is my iothubconnectionstring part
Hi Sunday, exception happens at regManager.GetDevicesAsync(), right? So you can create device(regManager.AddDeviceAsync) successfully?
– Rita Han - MSFT
Aug 27 at 7:33
@JoeyCai oh Thank you I will try that
– Sunday
Aug 27 at 8:13
@RitaHan-MSFT thank you but i tried but it cant to connect with iot hub
– Sunday
Aug 27 at 8:14
@JoeyCai I cant open that Solution on VS2013 , VS2015 and VS2017
– Sunday
Aug 27 at 8:20
1 Answer
1
I follow this tutorial to create an ASP.NET Core MVC application. Install Microsoft.Azure.Devices(version 1.17.0) from NuGet. Call the regManager.GetDevicesAsync() in the controller and get the result successfully.
ManageDevices.cs in Controllers:
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;
You can use the following URI to execute the GetDevices method:
https://localhost:44387/ManageDevices/GetDevices
The result of the got devices:
You can have a try to see if it helps.
Update:
You can access the following device datas:
Thank you for your reply. I tried to connect but I could not to get devices from iot hub. When i tried to connect internet directly I can get devices from iot hub. So i think the reason might be corporate network's proxy. Could you please consider again and let me know if you know anything.
– Sunday
Aug 31 at 8:02
@Sunday It seems a known issue and it has been fixed already for .net SDK. Here is a sample of how to set the proxy explicitly in the application. Can you have a try and let me know if it helps.
– Rita Han - MSFT
Sep 3 at 2:04
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The link you provided is about .net framework. It is different from asp.net core, you could refer to this article.
– Joey Cai
Aug 27 at 6:29