Updated on 2024-03-28 GMT+08:00

Application .NET SDK

IoTDA provides an application SDK in C# for developers. This topic describes how to install and configure the .NET SDK and how to use it to call application-side APIs.

Obtaining and Installing the SDK

  1. Install the .NET development environment.

    Visit the .NET website, and download and install the .NET development environment.
    The .NET SDK can be used in the following environments:
    • .NET Framework 4.5 and later
    • .NET Standard 2.0 and later
    • C# 4.0 and later

  2. Use the .NET CLI tool to install the SDK.

    dotnet add package HuaweiCloud.SDK.Core
    dotnet add package HuaweiCloud.SDK.IoTDA

Code Sample

The following code sample shows how to use the .NET SDK to call API Querying the Device List.

  1. Create a credential.
  2. Create and initialize a BasicCredentials instance.
  3. Instantiate a request object.
  4. Call the API for querying the device list.

    using System;
    using System.Collections.Generic;
    using HuaweiCloud.SDK.Core;
    using HuaweiCloud.SDK.Core.Auth;
    using HuaweiCloud.SDK.IoTDA;
    using HuaweiCloud.SDK.IoTDA.V5;
    using HuaweiCloud.SDK.IoTDA.V5.Model;
    
    namespace ListDevicesSolution
    {
        class Program
        {
            static void Main(string[] args)
            {
                var listDevicesRequest = ListDevices();
                var res = JsonUtils.Serialize(listDevicesRequest.Result);
                Console.WriteLine(res);
            }
    
           private static async Task<ListDevicesResponse> ListDevices()
            {
                // There will be security risks if the AK/SK used for authentication is directly written into code. Encrypt the AK/SK in the configuration file or environment variables for storage;
                // In this example, the AK/SK stored in the environment variables are used. Configure the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment first.
                var ak = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_AK", EnvironmentVariableTarget.Machine); 
                var sk = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_SK", EnvironmentVariableTarget.Machine);
                const string projectId = "<YOUR PROJECTID>";
               // region_id: If CN East-Shanghai1 is used, enter cn-east-3. If CN North-Beijing4 is used, enter cn-north-4. If CN South-Guangzhou is used, enter cn-south-1.
                const string regionId = "<YOUR REGION ID>";
                // endpoint: On the console, choose Overview and click Access Addresses to view the HTTPS application access address.
                const string endpoint = "<YOUR ENDPOINT>";
    
                // Create a credential.
                var auth = new BasicCredentials(ak, sk, projectId);
    
               // WithDerivedPredicate is used for the standard or enterprise edition. For the basic edition, delete the line.
                auth.WithDerivedPredicate(Credentials.DefaultDerivedPredicate);
    
                // Create and initialize an IoTDAClient instance.
                var client = IoTDAAsyncClient.NewBuilder()
                        .WithCredential(auth)
                       // For the standard or enterprise edition, create a region object.
                        .WithRegion(new Region(regionId, endpoint))
                       // For the basic edition, select the region object in IoTDARegion.
                       // .WithRegion(IoTDARegion.CN_NORTH_4)
                       // .NET Framework does not support content-type in the get request header.
                       // .WithHttpConfig(new HttpConfig().WithIgnoreBodyForGetRequest(true)) 
                        .Build();
    
                // Instantiate a request object.
                var req = new ListDevicesRequest
                {
                };
    
                try
                {
                    // Call the API for querying the device list.
                    var resp =await client.ListDevicesAsync(req);
                    Console.WriteLine(resp.GetHttpStatusCode());
                }
                catch (RequestTimeoutException requestTimeoutException)
                {
                    Console.WriteLine(requestTimeoutException.ErrorMessage);
                }
                catch (ServiceResponseException clientRequestException)
                {
                    Console.WriteLine(clientRequestException.HttpStatusCode);
                    Console.WriteLine(clientRequestException.ErrorCode);
                    Console.WriteLine(clientRequestException.ErrorMsg);
                }
                catch (ConnectionException connectionException)
                {
                    Console.WriteLine(connectionException.ErrorMessage);
                }
                return new ListDevicesResponse();
            }
        }
    }
    

    Parameter

    Description

    ak

    Access key ID of your Huawei Cloud account. You can create and view an AK/SK on the My Credentials > Access Keys page of the Huawei Cloud management console. For more information, see Access Keys.

    sk

    Secret access key (SK) of your Huawei Cloud account.

    IoTDARegion.CN_NORTH_4

    Region where the IoT platform to be accessed is located. The available regions of the IoT platform have been defined in the SDK code IoTDARegion.cs.

    On the console, you can view the region name of the current service and the mapping between regions and endpoints. For details, see Platform Connection Information.

Additional Information

For details on the project source code and usage guide, see Huawei Cloud .Net Software Development Kit (.Net SDK).