Install the IoT .NET SDK

  1. Install a .NET development environment.
    Recommendations:
    • Microsoft .NET Framework 4.0 and later.
    • Visual Studio 2010 and later.
  2. Download Alibaba Cloud .NET SDK packages.

    Click IoT.NET SDK releases to download the IoT .NET SDK package and .NET SDK core to download the Alibaba Cloud core library package, and then extract the DLL files from the packages.

  3. Import Alibaba Cloud .NET SDK libraries.
    1. In the Solution Explorer of Visual Studio, right-click your project, and select Reference.
    2. Click Add Reference.
    3. Click Browse in the dialog box.
    4. Select the DDL file aliyun-net-sdk-Iot.dll and aliyun-net-sdk-Core.dll, and click OK.

Initialize the SDK

Note The following example uses the China (Shanghai) region and the corresponding endpoint. You must specify the region of your IoT Platform and the endpoint of your service to use the SDK.
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
DefaultProfile.AddEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
IClientProfile clientProfile = DefaultProfile.GetProfile("cn-shanghai", "<your-access-key-id>", "<your-access-key-secret>");
DefaultAcsClient client = new DefaultAcsClient(clientProfile);

Create or view your AccessKeyId and AccessKeySecret on the AccessKey page in the console.

Initiate a call

In this example, call the Pub API to send messages to a topic.

PubRequest request = new PubRequest();
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);
}