Hi Team
I am running backend application on this module(C#), both on my CMD to check if it connect to my IOT hub, im getting a device not connection exception is being not found.
On my IOT hub i have device called "MyDotnetDevice, but if i use DeviceExolore to test for connecting to my IOT, it sends the message well and can view it on the portal as well on Azure. The code is as follows, but the question is why am i getting this exception both my CMD and VS IDE;
C:\Users\Gcobanim\source\repos\azure-iot-samples-csharp-master\azure-iot-samples-csharp-master\iot-hub\Quickstarts\back-end-application>dotnet run
IoT Hub Quickstarts #2 - Back-end application.
Unhandled Exception: Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException: Device {"Message":"{\"errorCode\":404103,\"trackingId\":\"3a490c8ca97b4ea88a6965bc61d5af5a-G:0-TimeStamp:09/13/2019 10:27:36-G:4-TimeStamp:09/13/2019 10:27:36\",\"message\":\"Timed out waiting for device to subscribe.\",\"info\":{},\"timestampUtc\":\"2019-09-13T10:27:36.06523Z\"}","ExceptionMessage":""} not registered
at Microsoft.Azure.Devices.HttpClientHelper.ExecuteAsync(HttpClient httpClient, HttpMethod httpMethod, Uri requestUri, Func`3 modifyRequestMessageAsync, Func`2 isMappedToException, Func`3 processResponseMessageAsync, IDictionary`2 errorMappingOverrides, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.HttpClientHelper.PostAsync[T,T2](Uri requestUri, T entity, TimeSpan operationTimeout, IDictionary`2 errorMappingOverrides, IDictionary`2 customHeaders, CancellationToken cancellationToken)
at back_end_application.BackEndApplication.InvokeMethod() in C:\Users\Gcobanim\source\repos\azure-iot-samples-csharp-master\azure-iot-samples-csharp-master\iot-hub\Quickstarts\back-end-application\BackEndApplication.cs:line 27
at back_end_application.BackEndApplication.Main(String[] args) in C:\Users\Gcobanim\source\repos\azure-iot-samples-csharp-master\azure-iot-samples-csharp-master\iot-hub\Quickstarts\back-end-application\BackEndApplication.cs:line 39
C# code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Azure.Devices.Client;
- using Microsoft.Azure.Devices;
- using Microsoft.Azure.Devices.Common.Exceptions;
- namespace BackEndApplication
- {
- class Program
- {
- private static ServiceClient s_serviceClient;
- private readonly static string s_connectionString = "HostName=UniversityIOTHub.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=r***=";
- private static async Task InvokeMethod()
- {
- var methodInvocation = new CloudToDeviceMethod("SetTelemetryInterval") { ResponseTimeout = TimeSpan.FromSeconds(30) };
- methodInvocation.SetPayloadJson("10");
- var response = await s_serviceClient.InvokeDeviceMethodAsync("MyDotnetDevice", methodInvocation);
- Console.WriteLine("Response status:{0}, payload", response.Status);
- Console.WriteLine(response.GetPayloadAsJson());
- }
- static void Main(string[] args)
- {
- Console.WriteLine("IOT Hub Test-- BackEndApplication.\n");
- s_serviceClient = ServiceClient.CreateFromConnectionString(s_connectionString);
- InvokeMethod().GetAwaiter().GetResult();
- Console.WriteLine("Press Enter to exit.");
- Console.ReadLine();
- }
- }
- }
Saravanan GanesanPosted Aug 4, 2023, 7:57 PM
The "DeviceNotFoundException" exception occurs because the device named "MyDotnetDevice" is not registered in your IoT Hub when running the C# code.
To resolve this issue, ensure the following:
Check the spelling and case sensitivity: Make sure the device name provided in the code ("MyDotnetDevice") matches the actual registered device name in your IoT Hub.
Verify the connection string: Double-check the correctness of the connection string ("s_connectionString") to ensure it is pointing to the correct IoT Hub and has the appropriate permissions for device registration and method invocation.
Register the device: If the device "MyDotnetDevice" is not registered in your IoT Hub, you need to register it first before using the C# code to invoke methods on it. You can register the device programmatically or through the Azure portal.
Verify device status: Ensure that the device "MyDotnetDevice" is active and connected to your IoT Hub. If it is not active or disconnected, the method invocation will fail.
Check IoT Hub status: Verify that your IoT Hub is running and operational without any issues that may prevent device registration or method invocation.
After confirming these points, try running the code again. If the device is registered and active, and the connection string is correct, the C# code should be able to invoke the "SetTelemetryInterval" method on the device successfully.
Guest UserPosted Sep 16, 2019, 4:45 AM
Sundaram SubramanianPosted Sep 14, 2019, 12:11 AM
Guest UserPosted Sep 13, 2019, 7:30 AM
Sundaram SubramanianPosted Sep 13, 2019, 6:08 AM