Use IoT Platform SDK for .NET

更新时间:
复制 MD 格式

IoT Platform provides developers with an SDK for .NET. This topic describes how to install and configure IoT Platform SDK for .NET. This topic also describes how to use the SDK to call the API operations of IoT Platform.

Install IoT Platform SDK for .NET

  1. Install .NET development environments.

    IoT Platform SDK for .NET supports the following development environments:

    • .NET Framework 4.5 and later

    • .NET Standard 2.0 and later

    • C# 4.0 and later

    • Visual Studio 2010 and later

  2. Install IoT Platform SDK for .NET by using the NuGet package manager.

    In this example, Visual Studio is used.

    1. In the Solution Explorer panel of Visual Studio, right-click your project and select Manage NuGet Packages.

    2. In the NuGet Package Manager panel, click Browse.

    3. On the Browse tab, enter aliyun-net-sdk in the search box and select the aliyun-net-sdk-iot package that is provided by Alibaba Cloud.

    4. Click Install.

Initialize IoT Platform SDK for C++

The following code shows an example of how to initialize the SDK to call an API in the China (Shanghai) region.

Create the clientProfile object to store SDK initialization information. Then, create the client instance from DefaultAcsClient. Use the DefaultAcsClient(clientProfile) method to load the SDK initialization information.

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;

string accessKeyId = Environment.GetEnvironmentVariable("ALIYUN_ACCESS_KEY_ID");
string accessKeySecret = Environment.GetEnvironmentVariable("ALIYUN_ACCESS_KEY_SECRET");

IClientProfile clientProfile = DefaultProfile.GetProfile("cn-shanghai", accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(clientProfile);

The clientProfile object is used to store the SDK initialization information.

  • : the ID of region where you activated IoT Platform.

    You can view the region in the upper-left corner of the IoT Platform console.

    For more information about region IDs, see Supported regions.

Initiate a request

The SDK encapsulates two classes for each API operation. The class names are in the ${API operation name}+"Request" and ${API operation name}+"Response" formats.

  • ${API operation name}+"Request": This class is used to call an API operation. You can use the request instance of this class and call the request.${request parameter name} method to configure the request parameters.

  • ${API operation name}+"Response"You can call the GetAcsResponse(request) method of the client instance that is created from DefaultAcsClient to obtain a response. You can call the response response.${response parameter name} method of the response instance of the ${API operation name}+"Response" class to obtain the values of the response parameters.

    For example, you can call the response.Success() method to obtain the value of the Success parameter. This parameter is a common response parameter that indicates whether a call is successful. Common response parameters also include the RequestId, ErrorMessage, and Code parameters.

For a list of IoT Platform cloud APIs, see API list. For descriptions of the request parameters in `request` and the response parameters in `response`, see the corresponding API reference.

This topic uses the Pub operation as an example to show how to publish a message to a topic. For information about the request parameters, see Pub.

Important

In the following code, ${iotInstanceId} represents the instance ID. You can find the ID of the current instance on the Instance Overview page in the IoT Platform console.

  • If an ID exists, you must pass this ID. Otherwise, the API call fails.

  • If the Instance Details page or the ID does not exist, you do not need to pass an ID. You can delete the request code related to `IotInstanceId` or pass an empty value `""`. Otherwise, the API call fails.

For more information about instances, see Instance overview. For information about how to purchase an instance, see Purchase an Enterprise instance. For frequently asked questions, see FAQ about IoT Platform instances.

PubRequest request = new PubRequest();
request.IotInstanceId = "<iotInstanceId>"; 
request.ProductKey = "<productKey>";
request.TopicFullName = "/<productKey>/<deviceName>/get";
byte[] payload = Encoding.Default.GetBytes("Hello World.");
String payloadStr = Convert.ToBase64String(payload);
request.MessageContent = payloadStr;
request.Qos = 0;
try
{
   PubResponse response = client.GetAcsResponse(request);
   Console.WriteLine("publish message result: " + response.Success);
   Console.WriteLine(response.ErrorMessage);
}
catch (ServerException e)
{
   Console.WriteLine(e.ErrorCode);
   Console.WriteLine(e.ErrorMessage);
}
catch (ClientException e)
{
   Console.WriteLine(e.ErrorCode);
   Console.WriteLine(e.ErrorMessage);
}

Appendix: Sample code

You can go to the IoT Platform Cloud SDK Example Center to view or download sample code for API calls. The sample code provides examples for the Java, Python, PHP, .NET, and Go SDKs.

Alibaba Cloud OpenAPI Explorer provides online debugging tools for API operations. On the API Debugging page, you can search for API operations, call API operations, and generate sample code for API operations of different SDKs. On the right side of the page, you can view the sample code of an SDK on the Sample Code tab. On the Debugging Result tab, you can view the actual request URL and response in the JSON format.