GithubHelp home page GithubHelp logo

azure / azure-iot-sdk-csharp Goto Github PK

View Code? Open in Web Editor NEW
453.0 94.0 495.0 57.99 MB

A C# SDK for connecting devices to Microsoft Azure IoT services

License: Other

C# 97.68% PowerShell 2.08% Batchfile 0.03% Bicep 0.13% Shell 0.08%

azure-iot-sdk-csharp's Introduction

Microsoft Azure IoT SDK for .NET

Contents

This repository contains the following:

  • Microsoft Azure IoT Hub device SDK for C# to connect client devices to Azure IoT Hub with .NET.
  • Microsoft Azure IoT Hub service SDK for C# to manage your IoT Hub service instance from a back-end .NET application.
  • Microsoft Azure Provisioning device SDK for C# to provision devices to Azure IoT Hub with .NET.
  • Microsoft Azure Provisioning service SDK for C# to manage your Provisioning service instance from a back-end .NET application.

Critical Upcoming Change Notice

All Azure IoT SDK users are advised to be aware of upcoming TLS certificate changes for Azure IoT Hub and Device Provisioning Service that will impact the SDK's ability to connect to these services. In October 2022, both services will migrate from the current Baltimore CyberTrust CA Root to the DigiCert Global G2 CA root. There will be a transition period beforehand where your IoT devices must have both the Baltimore and Digicert public certificates installed in their certificate store in order to prevent connectivity issues.

Devices with only the Baltimore public certificate installed will lose the ability to connect to Azure IoT hub and Device Provisioning Service in October 2022.

To prepare for this change, make sure your device's certificate store has both of these public certificates installed.

For a more in depth explanation as to why the IoT services are doing this, please see this article.

Recommended NuGet packages

Package Name Release Version
Microsoft.Azure.Devices.Client NuGet
Microsoft.Azure.Devices NuGet
Microsoft.Azure.Devices.Shared NuGet
Microsoft.Azure.Devices.Provisioning.Client NuGet
Microsoft.Azure.Devices.Provisioning.Transport.Amqp NuGet
Microsoft.Azure.Devices.Provisioning.Transport.Http NuGet
Microsoft.Azure.Devices.Provisioning.Transport.Mqtt NuGet
Microsoft.Azure.Devices.Provisioning.Service NuGet
Microsoft.Azure.Devices.Provisioning.Security.Tpm NuGet

Note:

  1. In addition to stable builds we also release pre-release builds that contain preview features. You can find details about the preview features released by looking at the release notes. It is not recommended to take dependency on preview NuGets for production applications as breaking changes can be introduced in preview packages.

  2. Device streaming feature is not being included in our newer preview releases as there is no active development going on in the service. For more details on the feature, see here.

    This feature has not been included in any preview release after 2020-10-14. However, the feature is still available under previews/deviceStreaming branch.

    The latest preview NuGet versions that contain the device streaming feature are:
    Microsoft.Azure.Devices.Client - 1.32.0-preview-001
    Microsoft.Azure.Devices - 1.28.0-preview-001

  3. Stable and preview NuGet versions are not interdependent; eg. for NuGet packages versioned 1.25.0 (stable release) and 1.25.0-preview-001 (preview release), there is no guarantee that v1.25.0 contains the feature(s) previewed in v1.25.0-preview-001. For a list of updates shipped with each NuGet package, please refer to the release notes.

The API reference documentation for .NET SDK is here.

To find SDKs in other languages for Azure IoT, please refer to the azure-iot-sdks repository. For IoT Hub Management SDK in .NET, please visit azure-sdk-for-net repository.

Developing applications for Azure IoT

Visit Azure IoT Dev Center to learn more about developing applications for Azure IoT.

Samples

All of our samples are located in this repository. The samples live alongside the source for each client library.

Samples for each of these categories are further separated into three sub-categories (from simplest to complex):

  1. Getting Started
  2. How To Guides
  3. Solutions

If you are looking for a good device sample to get started with, please see the device reconnection sample. It shows how to connect a device, handle disconnect events, cases to handle when making calls, and when to re-initialize the DeviceClient.

Contribute to the Azure IoT C# SDK

If you would like to build or change the SDK source code, please follow the devguide.

OS platforms and hardware compatibility

For an official list of all the operating systems and .NET platforms that we support, please see this document.

Note that you can configure your TLS protocol version and ciphers by following this document.

Key features and roadmap

IoT Hub Device SDK

✔️ feature available ✖️ feature planned but not supported ➖ no support planned

Features mqtt mqtt-ws amqp amqp-ws https Description
Authentication ✔️ ✔️* ✔️ ✔️* ✔️* Connect your device to IoT Hub securely with supported authentication methods, including private key, shared access signature (SAS) token, X.509 self-signed, and X.509 certificate authority (CA) signed.
*IoT Hub only supports X.509 CA signed over AMQP and MQTT at the moment. X.509 CA authentication over web socket and HTTPS are not supported. When authenticating a device using shared access key (SAK) over MQTT, new connection will be setup as part of the SAS token refresh process; device communication will be briefly closed during refresh (for details, see device connection and messaging reliability documentation). X.509 certificate-based authentication process does not have the same refresh disruption. Also, X.509 certificate-based authentication is more secure than share access key authentication.
Send device-to-cloud message ✔️* ✔️* ✔️ ✔️ ✔️ Send device-to-cloud messages (max 256KB) to IoT Hub with the option to add application properties and system properties, and batch send.
*IoT Hub only supports batch send over AMQP and HTTPS at the moment. The MQTT implementation loops over the batch and sends each message individually.
Receive cloud-to-device messages ✔️* ✔️* ✔️ ✔️ ✔️ Receive cloud-to-device messages and read associated application and system properties from IoT Hub, with the option to complete/reject/abandon C2D messages.
*IoT Hub does not support the option to reject/abandon C2D messages over MQTT at the moment.
Device Twins ✔️* ✔️* ✔️* ✔️* IoT Hub persists a device twin for each device that you connect to IoT Hub. The device can perform operations like get twin tags, subscribe to desired properties.
*Send reported properties version and desired properties version are in progress.
Direct Methods ✔️ ✔️ ✔️ ✔️ IoT Hub gives you the ability to invoke direct methods on devices from the cloud. The SDK supports handler for method specific and generic operation.
Upload file to Blob ✔️ The user can use the device client to retrieve a SAS URI from IoT Hub (to use for file uploads), upload to Azure Storage blob using IoT Hub provided credentials (using a supported client library), and then use the device client to notify IoT Hub that a file upload has completed. File upload requires HTTPS connection, but can be initiated from client using any protocol for other operations.
Connection Status and Error reporting ✔️ ✔️ ✔️ ✔️ ✔️ Error reporting for IoT Hub supported error code.
Retry policies ✔️ ✔️ ✔️ ✔️ ✔️ Retry policy for unsuccessful device-to-cloud messages have three options: no try, exponential backoff with jitter (default) and custom.
Devices multiplexing over single connection ✔️ ✔️ ✔️
Connection Pooling - Specifying number of connections ✔️ ✔️ ✔️
IoT Plug and Play ✔️ ✔️ ✔️* ✔️* IoT Plug and Play lets you build smart devices that advertise their capabilities to Azure IoT applications. IoT Plug and Play devices don't require manual configuration when a customer connects them to IoT Plug and Play-enabled applications. You can read more here.
*Note: AMQP support is mainly targeted for Edge-based scenarios.

IoT Hub Service SDK

✔️ feature available ✖️ feature planned but not supported ➖ no support planned

Features Support Transport protocol used underneath Client to use Description
Identity registry (CRUD) ✔️ HTTP RegistryManager Use your backend app to perform CRUD operation for individual device or in bulk.
Query ✔️ HTTP RegistryManager Use your backend app to query for information on device twins, module twins, jobs and message routing.
Import/Export jobs ✔️ HTTP RegistryManager Use your backend app to import or export device identities in bulk.
Scheduled jobs ✔️ HTTP JobsClient Use your backend app to schedule jobs to update desired properties, update tags and invoke direct methods.
Cloud-to-device messaging ✔️ AMQP ServiceClient Use your backend app to send cloud-to-device messages in AMQP and AMQP-WS, and set up notifications for cloud-to-device message delivery.
Direct Methods operations ✔️ HTTP ServiceClient Use your backend app to invoke direct method on device.
File Upload Notifications ✔️ AMQP ServiceClient Use your backend app to receive file upload notifications.
IoT Hub Statistics ✔️ HTTP ServiceClient Use your backend app to get IoT hub identity registry statistics such as total device count for device statistics, and connected device count for service statistics.
Digital Twin Operations ✔️ HTTP DigitalTwinClient or RegistryManager Use your backend app to perform operations on plug and play devices. The operations include get twins, update twins and invoke commands. DigitalTwinClient is the preferred client to use.

Provisioning Device SDK

This repository contains provisioning device client SDK for the Device Provisioning Service.

✔️ feature available ✖️ feature planned but not supported ➖ no support planned

Features mqtt mqtt-ws amqp amqp-ws https Description
TPM Individual Enrollment ✔️ ✔️ ✔️ This SDK supports connecting your device to the Device Provisioning Service via individual enrollment using Trusted Platform Module. Please review the samples folder and this quickstart on how to create a device client. TPM over MQTT is currently not supported by the Device Provisioning Service.
X.509 Individual Enrollment ✔️ ✔️* ✔️ ✔️* ✔️ This SDK supports connecting your device to the Device Provisioning Service via individual enrollment using X.509 root certificate. Please review the samples and this quickstart folder on how to create a device client.
X.509 Enrollment Group ✔️ ✔️* ✔️ ✔️* ✔️ This SDK supports connecting your device to the Device Provisioning Service via individual enrollment using X.509 leaf certificate. Please review the samples folder on how to create a device client.

Note * WebSocket support for MQTT/AMQP is limited to .NET Framework 4.x.

Provisioniong Service SDK

This repository contains provisioning service client SDK for the Device Provisioning Service to programmatically enroll devices.

Feature Support Description
CRUD Operation with TPM Individual Enrollment ✔️ Programmatically manage device enrollment using TPM with the service SDK. Please visit the samples folder to learn more about this feature.
Bulk CRUD Operation with TPM Individual Enrollment ✔️ Programmatically bulk manage device enrollment using TPM with the service SDK. Please visit the samples folder to learn more about this feature.
CRUD Operation with X.509 Individual Enrollment ✔️ Programmatically manage device enrollment using X.509 individual enrollment with the service SDK. Please visit the samples folder to learn more about this feature.
CRUD Operation with X.509 Group Enrollment ✔️ Programmatically manage device enrollment using X.509 group enrollment with the service SDK. Please visit the samples folder to learn more about this feature.
Query enrollments ✔️ Programmatically query registration states with the service SDK. Please visit the samples folder to learn more about this feature.

Read more

Device Explorer is no longer supported. A replacement tool can be found here.

Certificates - Important to know

The Azure IoT Hub certificates presented during TLS negotiation shall be always validated using the appropriate root CA certificate(s).

Always prefer using the local system's Trusted Root Certificate Authority store instead of hardcoding the certificates.

A couple of examples:

Additional Information

For additional guidance and important information about certificates, please refer to this blog post from the security team.

Support

The Azure IoT Hub Device Client supported releases is outlined in the following table.

Refer to the Azure IoT Device SDK lifecycle and support for details on the different supported stages.

Release Category End-of-life
NuGet Active -

Need help?

  • Have a feature request for SDKs? Please post it on User Voice to help us prioritize.
  • Have a technical question? Ask on Stack Overflow with tag “azure-iot-hub”.
  • Need Support? Every customer with an active Azure subscription has access to support with guaranteed response time. Consider submitting a ticket and get assistance from Microsoft support team.
  • Found a bug? Please help us fix it by thoroughly documenting it and filing an issue on GitHub (C, Java, .NET, Node.js, Python).

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Microsoft collects performance and usage information which may be used to provide and improve Microsoft products and services and enhance your experience. To learn more, review the privacy statement.

azure-iot-sdk-csharp's People

Contributors

abhipsamisra avatar ancaantochi avatar andyk-ms avatar anhashia avatar anporumb avatar arturl avatar az-iot-builder-01 avatar azabbasi avatar bertkleewein avatar bikamani avatar brycewang-microsoft avatar cipop avatar damonbarry avatar davilu avatar dominicbetts avatar drwill-ms avatar ewertons avatar gmileka avatar iluican avatar jamdavi avatar jasmineymlo avatar jenoola avatar mamokarz avatar prmathur-microsoft avatar tameraw avatar timtay-microsoft avatar tmahmood-microsoft avatar varunpuranik avatar vinagesh avatar yzhong94 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

azure-iot-sdk-csharp's Issues

AMQP Support in C# PCL

From @vslepakov on April 13, 2016 7:9

Great to see that there is now a PCL for C#. Are us guys plan on supporting AMQP in this PCL? As far as I know only HTTP is possible now.

Copied from original issue: Azure/azure-iot-sdks#477

Running Microsoft.Azure.Devices in Azure Function

When setting up and Azure Function (C#) for Register Devices with IoT Hub I get the following error when running:

RegistryManager manager = RegistryManager.CreateFromConnectionString(connectionString);

Exception while executing function: Functions.registerdevice. Microsoft.Azure.Devices: Could not load file or assembly 'Microsoft.Azure.Amqp, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

project.json:

{
    "frameworks": {
        "net46": {
            "dependencies": {
                "Microsoft.Azure.Devices": "1.2.3"
            }
        }
    }
}

How to receive the message from Iot Hub and display on MVC View

I have followed a link on iot-hub-csharp-csharp-getstarted
https://docs.microsaoft.com/en-us/azure/iot-hub/iot-hub-csharp-getstarted

  1. With this I have created an Console App that sends messages to the simulated device in my IoT Hub.
  2. I have then created a ASP.Net MVC app to subscribe the data from the cloud.

My Requirement
I would like to subscribe the data being continuously sent by the device in IoT Hub and display it on a MVC View. Please show me some pointers or an example to do this.

Create a SAS for a service

Hi,

How can I create a SAS for a server to send a message to a specific device and only that?

I am thinking about something like: sr= <host_url>/devices/<device_id>
Atm I have generated a SAS with this resource, but I keep getting Unauthorized when trying to send amessage from the server side to the device.
Am I doing it wrong or does IoT accept only SAS for sending messages to all devices (like resource: <host_url>/devicebound)

Thanks,
George

AddDeviceAsync does not throw DeviceAlreadyExistsException

Hi,
iam using the sample code to register a new device

        try
        {
            azureDevice = await _registryManager.AddDeviceAsync(new Device(device.RegistrationId));
        }
        catch(DeviceAlreadyExistsException)
        {
            return DeviceResult.Failed();
        }

However, when the device is already registered the exception is not thrown. Instead azureDevice is null.

Empty message sent when using latest version with HTTP or AMQP

We're using a very simple code to push data from the C# SDK to our Azure IoT Hub. After upgrading to the latest version of the library, we're seeing empty messages being pushed to the IoT Hub, either using HTTP or AMQP.

With HTTP, a Fiddler trace looks like that:

POST https://jeremy-iot-hub.azure-devices.net/devices/deviceID/messages/events?api-version=2016-11-14 HTTP/1.1
Authorization: SharedAccessSignature sr=jeremy-iot-hub.azure-devices.net%2Fdevices%2FComX200RN-SIMAGENT-000000&sig=xxx%3D&se=yyy
User-Agent: Microsoft.Azure.Devices.Client/1.1.3
iothub-messageid: 10
iothub-app-DSP_MESSAGE_ID: 10
iothub-app-DSP_QA_BATCH_ID: 0
iothub-app-DSP_QA_MESSAGE_PREFIX: test13
Accept: application/json
Host: jeremy-iot-hub.azure-devices.net
Content-Length: 0

Our code looks like this:

var deviceClient = DeviceClient.Create("jeremy-iot-hub.azure-devices.net", new DeviceAuthenticationWithRegistrySymmetricKey(deviceId, device.Authentication.SymmetricKey.PrimaryKey), TransportType.Http1);

var jObject = new JObject
{
    {"MessagePrefix", MessagePrefix},
    {"Batch", batchId},
    {"Message", msgId}
};

string messageString = jObject.ToString();
Message message = new Message(Encoding.UTF8.GetBytes(messageString)) { MessageId = msgId.ToString() };
message.Properties.Add("MESSAGE_ID", msgId.ToString());
message.Properties.Add("QA_BATCH_ID", batchId.ToString());
message.Properties.Add("QA_MESSAGE_PREFIX", MessagePrefix);

await deviceClient.SendEventAsync(outgoingMessage);

The same code was working just fine with version 1.0.11.

SetMethodHandler not working when using MethodResponse constructor (int)

Hey guys,

Just found a bug on SetMethodHandler.

When setting an handler using SetMethodHandler and that handler returns "new MethodResponse(200)", it doesn't work server-side (reponse timeout exception is thrown), and on client-side an ArgumentNullException appears on the Debug console.
If instead, I use new MethodResponse(byte[], int) constructor it works!

(SDK Version: 1.2.1)

Thanks

Method handlers not called after device is disabled and enabled again

  • SDK: C#

  • Version: Microsoft.Azure.Devices.Client v1.2.1 (let me know if you need the versions of the other packages, as of 2017-01-27 they're all at the latest version)

  • Code snippet to reproduce the bug

      private static async Task Do()
      {
          const string deviceId = "...";
          const string deviceConnectionString = "...";
          const string managerConnectionString = "...";
          const string serviceClientConnectionString = "...";
    
          var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, Microsoft.Azure.Devices.Client.TransportType.Mqtt);
          var registryManager = RegistryManager.CreateFromConnectionString(managerConnectionString);
          var serviceClient = ServiceClient.CreateFromConnectionString(serviceClientConnectionString);
    
          deviceClient.SetMethodHandler("ping", HandlePing, null);
    
          Console.WriteLine("Send ping to enabled device");
          await SendPing(deviceId, serviceClient);
    
          Console.WriteLine("Disable device");
          await SetDeviceStatus(registryManager, deviceId, DeviceStatus.Disabled);
    
          Console.WriteLine("Send ping to disabled device");
          await SendPing(deviceId, serviceClient);
    
          Console.WriteLine("Enable device");
          await SetDeviceStatus(registryManager, deviceId, DeviceStatus.Enabled);
    
          Console.WriteLine("Send ping to re-enabled device");
          await SendPing(deviceId, serviceClient);
      }
    
      private static Task<MethodResponse> HandlePing(MethodRequest methodRequest, object userContext)
      {
          return Task.FromResult(new MethodResponse(new byte[0], 200));
      }
    
      private static async Task SetDeviceStatus(RegistryManager registryManager, string deviceId, DeviceStatus status)
      {
          var device = await registryManager.GetDeviceAsync(deviceId);
          device.Status = status;
          await registryManager.UpdateDeviceAsync(device);
      }
    
      private static async Task SendPing(string deviceId, ServiceClient serviceClient)
      {
          try
          {
              var method = new CloudToDeviceMethod("ping", TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(20));
              method.SetPayloadJson("{}");
              var result = await serviceClient.InvokeDeviceMethodAsync(deviceId, method);
              Console.WriteLine("Sending ping succeeded.");
          }
          catch (Exception e)
          {
              Console.WriteLine("Sending ping failed: " + e);
          }
      }
    
  • Logs

      Send ping to enabled device
      Sending ping succeeded.
      Disable device
      Send ping to disabled device
      Sending ping failed: Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException: Device {"Message":"ErrorCode:DeviceNotFound;Device unavailable","ExceptionMessage":"Tracking ID:5a4dcda3933f4649bfb2d4e8466859ef-G:11-TimeStamp:01/27/2017 06:41:33"} not registered
         at Microsoft.Azure.Devices.HttpClientHelper.<ExecuteAsync>d__30.MoveNext()
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at Microsoft.Azure.Devices.HttpClientHelper.<PostAsync>d__23`2.MoveNext()
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
         at IotTest.Program.<SendPing>d__4.MoveNext() in C:\Users\Hannes\Work\IotTest\IotTest\Program.cs:line 65
      Enable device
      Send ping to re-enabled device
      Sending ping failed: Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException: Device {"Message":"ErrorCode:DeviceNotOnline;Timed out waiting for device to connect.","ExceptionMessage":"Tracking ID:cc156c9e4ab9419cb5a2b5b916d90a40-G:11-TimeStamp:01/27/2017 06:41:53"} not registered
         at Microsoft.Azure.Devices.HttpClientHelper.<ExecuteAsync>d__30.MoveNext()
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at Microsoft.Azure.Devices.HttpClientHelper.<PostAsync>d__23`2.MoveNext()
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
         at IotTest.Program.<SendPing>d__4.MoveNext() in C:\Users\Hannes\Work\IotTest\IotTest\Program.cs:line 65
    

As you can see, method handlers don't work after re-enabling the device. However, message sending and receiving still works. Also method handlers work if deviceClient is disposed and recreated.

So a work-around would be to dispose and recreate the client every time an error occurs. But here's the next problem: the client doesn't really know that he got disabled.

Any ideas?

ErrorCode:DeviceNotOnline when invoking device method from DeviceExplorer to a MQTT device client

From @michael-chi on December 2, 2016 8:58

I am testing Device Method with a C# client using M2Mqtt library. following this doucment:https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support
my client subscribes to the topic "$iothub/methods/POST/#".

When invoking device method from Device Explorer, I got error message ErrorCode:DeviceNotOnline even when my client is able to send events to IoT Hub. The error came out immediately after I click "Call Method" button (does not honor Timeout setting in Device EXplorer)

my client code:
var deviceId = ConfigurationManager.AppSettings["deviceId"];
var iotHubName = ConfigurationManager.AppSettings["iotHubName"];
var iotHubUrl = $"{iotHubName}.azure-devices.net";

        var clientId = deviceId;
        var userName = $"{iotHubName}.azure-devices.net/{deviceId}";
        var password = ConfigurationManager.AppSettings["deviceSAS"];
        var publishPath = $"devices/{deviceId}/messages/events/";
        var subscribePath = $"devices/{deviceId}/messages/devicebound/#";
        
        var client = new MqttClient(iotHubUrl, 8883, true, null,null,MqttSslProtocols.TLSv1_0);
       
        var result = client.Connect(clientId, userName, password);
        var directMethodPath = "$iothub/methods/POST/#";
        client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
        
        
        client.Subscribe(new string[] { directMethodPath }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });

Copied from original issue: Azure/azure-iot-sdks#1007

How to handle connection issue during sending message in AMQP protocol

I investigate topic about handling connection issues with Azure IoT.
I need information how to handle connection issue during sending message (C2D) from ClientService object to Device using AMQP protocol.
I can easily test situation when Internet is disconnected and I try send message - I receive exception System.OperationCanceledException: The AMQP object link25 is aborted at Microsoft.Azure.Devices.AmqpServiceClient.d__27.MoveNext()
Test with Internet disconnection during transfer message is harder. I assume that protocol AMQP uses checksum so this situation should throw also some exception, but I'm not sure about this. I dont't know what exception should it throw and how to be sure that message won't be lost?

UWP version of Microsoft.Azure.Devices nuget package not compatible with Microsoft.Azure.Amqp

If you add Microsoft.Azure.Devices 1.1.5 to a UWP project with Microsoft.Azure.Amqp 1.1.5, calls to the RegistryManager will work fine. If you upgrade Microsoft.Azure.Amqp to 2.0.2, however, you get an exception:

System.IO.FileNotFoundException was unhandled by user code
FileName=Microsoft.Azure.Amqp.Uwp, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
HResult=-2147024894
Message=Could not load file or assembly 'Microsoft.Azure.Amqp.Uwp, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Source=Microsoft.Azure.Devices.Uwp
StackTrace:
at Microsoft.Azure.Devices.RegistryManager.CreateFromConnectionString(String connectionString)
at TestClient.share.TestClient.TestConnectionState()
at Tests.UWP.ClientTests.ConnectionStateTest()
InnerException:

If you do this with the .Net versions of the same packages, everything seems to work fine.

Trying to install the Microsoft.azure.devices 1.2.2 to a windows service

I am getting an error

Could not install package 'Microsoft.Azure.Devices 1.2.2'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Trying this in a windows service, but unable to do the same.

ScheduleDeviceMethodAsync not working properly

I'm able to schedule (queue) a new job using JobClient.ScheduleDeviceMethodAsync method, however, the result is always "Failed" with the following error:

SQL query parse failed due to following errors:
Line 1:29 Unexpected character at input 'SELECT': @Microsoft.Azure.Devices.DeviceManagement.JobService.Orchestrations.IMultipleDevicesOrchestrationManager.QueryDevicesAsync

The query that I'm sending is the same as in the documentation: "SELECT * FROM devices"

Am I doing something wrong? Or is the IoT service not working on Azure?

(SDK Version: 1.2.1)

Thanks!

Cannot access a disposed object. Object name: 'AmqpBodyStreamMessage'

Hi Guys,
I am facing the issue RANDOMLY(faced about one time when sending 200 messages), What the possible reason for the issue. Could someone help me here? thanks
//Create DeviceClient
_deviceClient = DeviceClient.CreateFromConnectionString(GetConnectionString(), TransportType.Amqp_WebSocket_Only);
//_deviceClient.OpenAsync() ; previously I did not call the OpenAsync, is this the possible reason?

        the exception is raised when sending messages 
        //Sending DeviceClient                          
        await _deviceClient.SendEventAsync(message);
        And I observed that it took about 100 seconds before the exception raised.

Thanks
Hotants
----Exception-------
Microsoft.Azure.Devices.Client.Transport.AmqpTransportHandler.d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.AmqpTransportHandler.d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.GateKeeperDelegatingHandler.d__12.MoveNext()

 The version of packages referenced as below:

DeviceExplorer returns error when using Call Method On Device form when using latest API version 2016-11-14

image

Error details:

Authorization: SharedAccessSignature sr=pilot-restore.azure-devices.net&sig=hidden&se=1484730554&skn=iothubowner
UserAgent: Microsoft.Azure.Devices/1.1.0-preview-004
Accept: application/json
Content-Type: application/json; charset=utf-8
Host: pilot-restore.azure-devices.net
Content-Length: 75

{"methodName":"MethodName","timeoutInSeconds":60,"payload":'MethodPayload'}
HTTP/1.1 404 Not Found
Content-Length: 180
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
iothub-errorcode: DeviceNotOnline
Date: Wed, 18 Jan 2017 08:11:45 GMT

{"Message":"ErrorCode:DeviceNotOnline;Timed out waiting for device to connect.","ExceptionMessage":"Tracking ID:66b8d59066b24105a49bffcbde18495d-G:5-TimeStamp:01/18/2017 08:11:45"}

Adding Microsoft.Azure.Devices nuget package to a C# UWP unit test project causes build to fail

Something about adding the package to unit test project causes it to look for a csproj file that doesn't exist, and I suspect shouldn't ever exist in a nuget package:

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\AppxPackage\Microsoft.AppXPackage.Targets(2156,5): error APPX0702: Payload file 'C:\Users\v-tbert.nuget\packages\Microsoft.Azure.Devices\1.1.5\lib\uap10.0\Microsoft.Azure.Devices.Uwp\Microsoft.Azure.Devices.Shared.UWP.csproj' does not exist

FWIW, it does not happen when Microsoft.Azure.Devices.CLIENT is added to the same project.

No UploadToBlobAsync Operations Monitoring correlation

Hi,

When using the UploadToBlobAsync operation the SDK performs three actions that are logged in operational monitoring, e.g.,

{"time":"2016-12-21T07:31:24.1350268Z","operationName":"sasUriRequest","category":"FileUploadOperations","level":"Information","deviceId":"device1"}
{"fileUploadStatusCode":0,"time":"2016-12-21T07:31:24.8878250Z","operationName":"fileUploadToStorage","category":"FileUploadOperations","level":"Information","deviceId":"device1"}
{"time":"2016-12-21T07:31:24.8878250Z","operationName":"completionRequest","category":"FileUploadOperations","level":"Information","deviceId":"device1"}

The problem with this, is that if there are a lot of devices all uploading files, it can become nearly impossible to correlate events that relate to a single invocation of the method. I want to do this to build a dashboard so I can see when things are failing, but would currently have to group by deviceId and some vague bracket of time to determine the number of events I have, or have something like a count of each event type against a deviceId over time.

What would be good would be either the filename to be included in the monitoring, or a correlationId, or even better both.

Thanks,

Martin

[DeviceExplorer] Timestamp corrupts the JSON payload

From @DanaEpp on November 10, 2016 22:28

If you select the "Add Time Stamp" to the "Message To Device" tab, when firing to the device it will blow up in ble_c2d.c on line 241. A sample error is:

Error: Time:Thu Nov 10 14:23:52 2016 File:/home/pi/code/azure-iot-gateway-sdk/modules/ble/src/ble_c2d.c Func:BLE_C2D_Receive Line:241 JSON Object expected, not received.

Copied from original issue: Azure/azure-iot-sdks#947

Multi-tenant IoT hub application

Hello,

Firstly I would like to indicate that I am well aware that this is not a SDK issue, but rather a question regarding application implementation. If this is not the place where such questions can be answered, then tell me at least where I can present my case.


I would like to implement a multi-tenant environment with IoT hub where I want to allow each tenant to connect directly to IoT hub (as service) to communicate with devices. Of course I would want each tenant to have access only to their own devices and not have the possibility to connect to devices from other tenants (thus I require the proper SAS).

Following the documentation on the ServiceConnect permissions, it would seem that it is not possible to do this restriction on a SAS level. I am expecting an URL like "{host}/devices/{device_id}/...." (where .... is devicebound or messages or something), but did not find a sample and the piece of code that I came up with result in Unauthorized.

I would like to know what strategies can I use to allow this multi-tenant connection to IoT hub. Did anyone manage to create a SAS with ServiceConnect to device specific URL? Is it even possible or what other workarounds are there for this scenario?

Also, if you have any samples with generating a SAS that allows me to use the endpoint {host}/messages/devicebound, I would appreciate it a lot.


Thanks,
George

SetDesiredPropertyUpdateCallback() is not working.

Using master from https://github.com/Azure/azure-iot-sdk-csharp.git, the callback function is not getting called when the Device Twin desired properties change.

Here's the WPF application code I have tested with:

    public partial class MainWindow : Window
{
private const string DeviceConnectionString = "";
private DeviceClient _deviceClient;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void OnTest(object sender, RoutedEventArgs e)
    {
        _deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
        _deviceClient.SetDesiredPropertyUpdateCallback(OnDesiredPropertyUpdate, this);
        _deviceClient.SetMethodHandlerAsync("MyMethodAsync", MyMethodAsync, null);

    }

    public Task OnDesiredPropertyUpdate(TwinCollection desiredProperties, object userContext)
    {
        Debug.WriteLine("Property!");
        return null;
    }

    Task<MethodResponse> MyMethodAsync(MethodRequest methodRequest, object userContext)
    {
        Debug.WriteLine("Method!");
        return null;
    }
}

Cannot load resources if running as UWP background task

When trying to get a resource string, a customer is getting:

{"Resource Contexts may not be created on threads that do not have a CoreWindow. (Exception from HRESULT: 0x80073B27)"}
   at Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView()
   at Microsoft.Azure.Devices.Client.Common.ResourceManagerImpl..ctor()
   at Microsoft.Azure.Devices.Client.Common.Resources..cctor()

This is likely caused by the fact that background tasks don't have "views". The suggestion is to call GetForViewIndependentUse instead of GetForCurrentView.

Information about auto reconnect, ping time

Hello,
I couldn't find any documentation with information how ServiceClient/DeviceClient reconnect to IoTHub after disconnection and connection system to Internet.
I have found some information on azure-iot-sdks GitHub issues:
https://github.com/Azure/azure-iot-sdks/issues/653 - moved to Azure/azure-iot-sdk-java#10
https://github.com/Azure/azure-iot-sdks/issues/748

Can you give me any source or documentation with information about ping time used in AMQP:

I also made some tests with azure-iot-sdk-csharp and AMQP protocol:

  • first I connected ServiceClient to IoTHub and started receiving feedbacks
  • I disconnected Internet
  • after few minutes method to receive feedback got exception:

System.OperationCanceledException: The operation was canceled. at Microsoft.Azure.Devices.AmqpFeedbackReceiver.d__15.MoveNext()
and then few times per second application throws this:
System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server ---> System.Net.WebException: The remote name could not be resolved: 'XXXXX.azure-devices.net'

  • connecting to Internet immediately resolved problem with exceptions and after about 3-5 minutes I received message feedback - so reconnect to IoT Hub works.

Exception on the file uploading with a x509 certificate device

I got an exception on UploadFileToBlob method in the x509 certificate device.
Does it support file upload feature in the x509 device?

(Microsoft.Azure.Devices.Client) Version: 1.2.3

Here is the log:

Type of Exception : ArgumentException
Message : '' is not a valid Base64 encoded string.
Parameter name: Key
Source : Microsoft.Azure.Devices.Client
Stack Trace : at Microsoft.Azure.Devices.Client.StringValidationHelper.EnsureBase64String(String value, String paramName)
at Microsoft.Azure.Devices.Client.IotHubConnectionString.BuildToken(TimeSpan& ttl)
at Microsoft.Azure.Devices.Client.IotHubConnectionString.GetPassword()
at Microsoft.Azure.Devices.Client.IotHubConnectionString.GetAuthorizationHeader()
at Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.HttpClientHelper.d__16`2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.HttpTransportHandler.d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.SmartFactory.Devices.Client.SfDeviceClient.d__16.MoveNext() in C:\Users\a-walin\Github\MOEA-SmartFactory\sfDeviceLib\CSSDK\SfDeviceClient.cs:line 110
TargetSite : Void EnsureBase64String(System.String, System.String)

Thanks for the help.

Device ConnectionState does not get set when using UWP versions of nuget packages

When using the .Net version of the nuget packages Microsoft.Azure.Devices and Microsoft.Azure.Devices.Client, the code below sets the ConnectionState property correctly. However, when you use the exact same code from a UWP project with the respective nuget packages, the property never gets set.

`
public static void TestConnectionState()
{
var maxSeconds = 30;
var deviceManager = RegistryManager.CreateFromConnectionString(hubOwnerConnectionString);
var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString);
var message = new Microsoft.Azure.Devices.Client.Message();

		deviceManager.OpenAsync().Wait();
		var device = deviceManager.GetDeviceAsync(deviceId).Result;
		if (device?.ConnectionState != DeviceConnectionState.Disconnected)
			throw new Exception();

		deviceClient.OpenAsync().Wait();
		deviceClient.SendEventAsync(message).Wait();

		int counter = 0;
		do
		{
			Task.Delay(1000).Wait();
			device = deviceManager.GetDeviceAsync(deviceId).Result;
		} while (++counter < maxSeconds && device.ConnectionState != DeviceConnectionState.Connected);

		if (device.ConnectionState != DeviceConnectionState.Connected)
			throw new TimeoutException();
		else
			Debug.WriteLine($"It took about {counter} seconds for the state to change.");

		try
		{
			deviceClient.CloseAsync().Wait();
		}
		catch (AggregateException ex)
		{
			//why isn't this universally implemented???
			if (! (ex.InnerException is NotImplementedException))
				throw;
		}
	}

`

Method UploadToBlobAsync on DeviceClient throws exception if DeviceId contain special characters

According to documentation: https://docs.microsoft.com/pl-pl/azure/iot-hub/iot-hub-devguide#device-identity-registry
DeviceId should be case-sensitive string (up to 128 characters long) of ASCII 7-bit alphanumeric characters + special characters {'-', ':', '.', '+', '%', '_', '#', '*', '?', '!', '(', ')', ',', '=', '@', ';', '$', '''}
I tried to send file using UploadToBlobAsync method on DeviceClient object where DeviceID contain '#' or '?' character and I received exception.

Exception message:

Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (403) Forbidden. ---> System.Net.WebException: The remote server returned an error: (403) Forbidden.
at Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException[T](HttpStatusCode expectedStatusCode, HttpStatusCode actualStatusCode, T retVal, StorageCommandBase1 cmd, Exception ex) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\Common\Shared\Protocol\HttpResponseParsers.Common.cs:line 50 at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.<>c__DisplayClass42.<PutBlobImpl>b__41(RESTCommand1 cmd, HttpWebResponse resp, Exception ex, OperationContext ctx) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 2339
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndGetResponse[T](IAsyncResult getResponseResult) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 299
--- End of inner exception stack trace ---
at Microsoft.Azure.Devices.Client.Transport.HttpTransportHandler.d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at SimulatedDevice.SimulatedDeviceForm.<sendFileButton_Click>d__11.MoveNext() in C:[myPathToProject].cs:line 116
Request Information
RequestID:d0792532-0001-00b6-8010-6520a4000000
RequestDate:Mon, 02 Jan 2017 15:54:15 GMT
StatusMessage:Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
ErrorCode:AuthenticationFailed

How to mock Microsoft.Azure.Devices.ServiceClient and RegistryManager

Hello
Im trying to test my service for iot hub.
When im running my test im getting following error:

Result StackTrace:
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type proxyType, List1 proxyArguments, Type classToProxy, Object[] constructorArguments) at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors) at Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments) at Moq.Mock1.b__24_0()
at Moq.PexProtector.Invoke(Action action)
at Moq.Mock1.InitializeInstance() at Moq.Mock1.OnGetObject()
at Moq.Mock.GetObject()
at Moq.Mock.get_Object()
at Moq.Mock`1.get_Object()
at IoTHubServiceTest.Setup() in IoTHubServiceTest.cs:line 32
Result Message:
Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: Microsoft.Azure.Devices.ServiceClient.
Could not find a parameterless constructor.

This is my test class.

 public class IoTHubServiceTest
    {
        private Mock<ServiceClient> serviceClientMock;
        private IoTHubService ioTHubService;

        /// <summary>
        /// The start up.
        /// </summary>
        [SetUp]
        public void Setup()
        {        
            this.serviceClientMock = new Mock<ServiceClient>();
            ioTHubService = new IoTHubService(serviceClientMock.Object);
        }

        [Test]
        [Category("IoTMgt")]
        public async Task TestSendCloudToDeviceMessageAsyncShouldCall()
        {
            await ioTHubService.SendCloudToDeviceMessageAsync("TestDevice", "TestMessage");

            serviceClientMock.Verify(p => p.SendAsync(It.IsAny<string>(), It.IsAny<Message>()), Times.Once());
        }

    }
``

My question is how can i mock ServiceClient?
I want to test that my method called ServiceClient.SendAsync().

Thank you.
Bartek

Robust upload of large files

Azure supports uploading large files in chunks to support continuing broken uploads. Do you plan to support this in the SDK as well?

I could also imagine having the user implement this and just providing a hook to doing something with a CloudBlockBlob:

public Task UploadBlobAsync(string blobName, Func<CloudBlockBlob, Task> upload);

small bug with the Device Explorer tool UpdateTwinAsync is not awaited

On class DeviceTwinAndMethod
Line 82:
registryManager.UpdateTwinAsync(dp.DeviceId, dp, dp.ETag);

UpdateTwinAsync not awaited. There is a delay for one second, but if there is a problem it will not be catched.

change to:
await registryManager.UpdateTwinAsync(dp.DeviceId, dp, dp.ETag);

Application properties are not implemented

Apologies if this is incorrect but having looked through the code and documentation it doesn't seem that the C# library implements the setting of ApplicationProperties for Messages (despite being implemented within MessageConverter). Is this missing or am I simply missing how it was implemented?

Executing ConnectTheDots

I have downloaded the code ConnectTheDots http://connectthedots.io
I am able to successfully comiple & build it.

To execute this app do I need to have the below ?
1. Windows 8 or above
2. IIS 8

I am having Windows 7 and IIS 7. Will I be able to execute with this system spec
If no are there any updates that I can run in my system to make this workable in my system?

Remove user context from SetMethodHandler

The last parameter in SetMethodHandler is unnecessary. It's a throwback to the 80-s style of programming with void*. There are three problems with it:

  • There are many cases where it's not necessary, so the user has to pass null. This is an invitation for NullReferenceExceptions.
  • In the callback, the user is forced to downcast the object to the expected type, which can be a source of errors (the actual and expected types must be kept in sync)
  • It makes the implementation more complex (it needs to maintain Tuple<MethodCallback, object> instead of just MethodCallback)

The context can be passed into the callback with the help of a lambda:

deviceClient.SetMethodHandler("CallMe", _ => CallMe(_, new MyType()));
...
void CallMe(MethodRequest methodRequest, MyType mytype)
{
    // use mytype directly without having to downcast it from object
}

C# devs are quite capable of this nowadays.

Best practise for SAS rotation

What is the preferred way when we want to rotate SAS's regularly? Should the device then have two connection strings (one with primary and one with secondary key) and just try both of them? Is something like this supported in the SDK or is it even a common scenario?

Support for net45 framework

From @danstoian on October 3, 2016 5:48

Hei,

Can you please compile the Microsoft.Azure.Devices package for net45?
Our project targets net45 (and we cannot change it).

Microsoft.Azure.Amqp requires net451 and I've submitted a pull-request to change the target framework from net451 to net45
(Azure/azure-amqp#39)

Thanks,
Dan

Copied from original issue: Azure/azure-iot-sdks#853

Nuget dependency not set for Microsoft.Azure.Amqp

I have a simple UWP app that uses the Microsoft.Azure.Devices nuget package and the first call to RegistryManager fails. I get a file not found exception presumably because the nuget dependency for Microsoft.Azure.Amqp wasn't set.

System.IO.FileNotFoundException was unhandled by user code
FileName=Microsoft.Azure.Amqp.Uwp, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
HResult=-2147024894
Message=Could not load file or assembly 'Microsoft.Azure.Amqp.Uwp, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Source=Microsoft.Azure.Devices.Uwp
StackTrace:
at Microsoft.Azure.Devices.RegistryManager.CreateFromConnectionString(String connectionString)
at TestClient.share.TestClient.TestConnectionState()
at App1.App.OnLaunched(LaunchActivatedEventArgs e)
InnerException:

TransportType.Mqtt not implemented in UWP version of Microsoft.Azure.Devices.Client

Mqtt transport doesn't seem to be implemented in UWP version of Microsoft.Azure.Devices.Client. I believe this means that important functionality like Direct Methods and Jobs are not supported from UWP

System.NotImplementedException was unhandled by user code
HResult=-2147467263
Message=Mqtt protocol is not supported
Source=Microsoft.Azure.Devices.Client.UWP
StackTrace:
at Microsoft.Azure.Devices.Client.DeviceClient.CreateFromConnectionString(String connectionString, TransportType transportType, IDeviceClientPipelineBuilder pipelineBuilder)
at TestClient.share.TestClient.TestConnectionState()
at Tests.UWP.ClientTests.ConnectionStateTest()
InnerException:

UWP - Exception thrown with message "Transient error occured, please retry"

I have developed an UWP app containing the following method:

private async Task sendToAzure(string message)
{
try
{
string DeviceConnectionString = "HostName=XXXXXXX.azure-devices.net;DeviceId=YYYYYYY;SharedAccessKey=ZZZZZZZZZZZZ";
DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Http1);

            Message eventMessage = new Message(System.Text.Encoding.UTF8.GetBytes(message));
            await deviceClient.SendEventAsync(eventMessage);
            Debug.WriteLine("Send message to Azure completed\n");
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Exception : "+ ex.Message + " while trying to send the Json message:");
            Debug.WriteLine(message);
        }

}

When the app runs the following exception is thrown:

Exception thrown: 'System.Net.Http.HttpRequestException' in mscorlib.ni.dll
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException' in Microsoft.Azure.Devices.Client.UWP.dll
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException' in mscorlib.ni.dll
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException' in mscorlib.ni.dll
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in Microsoft.Azure.Devices.Client.UWP.dll
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll
Exception : Transient error occured, please retry.

I would really appreciate any help.
Regards, Maria.

CONNECT failed: RefusedNotAuthorized C#

{Microsoft.Azure.Devices.Client.Exceptions.UnauthorizedException: CONNECT failed: RefusedNotAuthorized
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.Mqtt.MqttTransportHandler.

When I try to set method handlers I get the above inner exception.
csharpcode.txt

SetMethodHandler does not handle errors correctly

SetMethodHandler is a void method but calls a method returning Task internally, without awaiting the result:

// codes_SRS_DEVICECLIENT_10_005: [ The SetMethodHandler shall EnableMethodsAsync when called for the first time. ]
ApplyTimeout(operationTimeoutCancellationToken => this.InnerHandler.EnableMethodsAsync(operationTimeoutCancellationToken));

If EnableMethodsAsync fails, the errors are ignored and DeviceCient object gets into an invalid state.

To simulate EnableMethodsAsync failure, you can create the DeviceClient object with AMQP protocol.

I can think of a few ways to solve it, if you want to discuss let me know.

.net core support

From @ondrejtomcik on October 22, 2016 20:44

Hello,
we're using .net core and I am missing support for Microsoft.Azure.Devices . When it will be added please?
Service fabric already supports .net core but this main IoT SDK, which should be first in my opinion, is still missing.

Thanks

Copied from original issue: Azure/azure-iot-sdks#887

Issue in receiving data continuously from IoT Hub

My Issue
I am having issues on receiving data continuously from IoT Hub.

I have used iot-hub-csharp-csharp-getstarted as reference to create my ASPNet MVC app

Below is the function to receive data from IoT Hub

private static async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken ct)
    {
        var eventHubReceiver = eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partition, DateTime.UtcNow);
        while (true)
        {
            if (ct.IsCancellationRequested) break;
            EventData eventData = await eventHubReceiver.ReceiveAsync();
            if (eventData == null) continue;

            string data = Encoding.UTF8.GetString(eventData.GetBytes());

            //Console.WriteLine("Message received. Partition: {0} Data: '{1}'", partition, data);

        }
    }

The value is getting received in var string data But it comes out of the loop after some time (whereas in the Console app which sends data to IoT hub, it keeps continuously sending data )

The above function is called via the below one

private void SetHubs()
        {
            eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, iotHubD2cEndpoint);
            var d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;
            CancellationTokenSource cts = new CancellationTokenSource();

            var tasks = new List<Task>();
            foreach (string partition in d2cPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
            }
            //Task.WaitAll(tasks.ToArray());


        }

My problem

So in the receiving MVC app (using the above method ReceiveMessagesFromDeviceAsync, it comes out of the loop (though it is inside while loop ==>while(true)) Only some 50 - 70 message are received after which it exists the while loop, What is that I am missing here?

Update

I tried including try catch block
i.e placing the code statements - while(true) part - of the method ReceiveMessagesFromDeviceAsync in try catch block as below.
I have confirmed that it never hits the statements marked below as ====>
STT-A or STT-B or STT-E or STT_END_OF_METHOD
It loops inside while for some number of times (i.e receives data from IoT Hub)
and at one point of time at the statement STT- IoT_DATA it comes out of the method and returns to the calling method - i.e to the method SetHubs() I have mentioned in my query\comment
(This proceeds to display the page)

try{
          if (ct.IsCancellationRequested) 
                break; // STT-A
          EventData eventData = await eventHubReceiver.ReceiveAsync(); // STT- IoT_DATA
          if (eventData == null) 
                 continue;//STT-B
          string data = Encoding.UTF8.GetString(eventData.GetBytes());
}
catch(Expecption exp){
string e =exp.message(); //STT-E
}
} //STT-END_OF_METHOD

Am I missing something like the partition limit or so. If so please guide me how to do or make the required settings to make this work.

Note : The same code works fine in a Console App.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.