Version 4 (V4) of the AWS SDK for .NET has been released!
For information about breaking changes and migrating your applications, see the migration topic.
AWS IoT data examples using SDK for .NET (v4)
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for .NET (v4) with AWS IoT data.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use GetThingShadow.
- SDK for .NET (v4)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /// <summary> /// Gets the Thing's shadow information. /// </summary> /// <param name="thingName">The name of the Thing.</param> /// <returns>The shadow data as a string, or null if retrieval failed.</returns> public async Task<string?> GetThingShadowAsync(string thingName) { try { var request = new GetThingShadowRequest { ThingName = thingName }; var response = await _amazonIotData.GetThingShadowAsync(request); using var reader = new StreamReader(response.Payload); var shadowData = await reader.ReadToEndAsync(); _logger.LogInformation($"Retrieved shadow for Thing {thingName}"); return shadowData; } catch (Amazon.IotData.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot get Thing shadow - resource not found: {ex.Message}"); return null; } catch (Exception ex) { _logger.LogError($"Couldn't get Thing shadow. Here's why: {ex.Message}"); return null; } }-
For API details, see GetThingShadow in AWS SDK for .NET API Reference.
-
The following code example shows how to use UpdateThingShadow.
- SDK for .NET (v4)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /// <summary> /// Updates the Thing's shadow with new state information. /// </summary> /// <param name="thingName">The name of the Thing.</param> /// <param name="shadowPayload">The shadow payload in JSON format.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> UpdateThingShadowAsync(string thingName, string shadowPayload) { try { var request = new UpdateThingShadowRequest { ThingName = thingName, Payload = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(shadowPayload)) }; await _amazonIotData.UpdateThingShadowAsync(request); _logger.LogInformation($"Updated shadow for Thing {thingName}"); return true; } catch (Amazon.IotData.Model.ResourceNotFoundException ex) { _logger.LogError($"Cannot update Thing shadow - resource not found: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't update Thing shadow. Here's why: {ex.Message}"); return false; } }-
For API details, see UpdateThingShadow in AWS SDK for .NET API Reference.
-