Updated on 2026-11-10 GMT+08:00

.NET SDK Calling Example

This section uses the API for querying the instance list (ListInstances) as an example to describe how to use the Huawei Cloud .NET SDK.

Prerequisites

  • You have obtained the access key (AK) and secret access key (SK) corresponding to your Huawei Cloud account. For details, see Creating Access Keys.
  • .NET Framework 4.7.2 or later, or .NET 8.0 or later has been installed in the environment where the SDK runs.

Installing the SDK

You can install the Huawei Cloud .NET SDK using the following methods: The .NET SDK 3.1.213 is used as an example. For details about the latest SDK version, visit SDK Center.

  • Installing the SDK using .NET CLI
    dotnet add package HuaweiCloud.SDK.Core --version 3.1.213
    dotnet add package HuaweiCloud.SDK.Kafka --version 3.1.213
  • Installing the SDK using Package Manager
    Install-Package HuaweiCloud.SDK.Core -Version 3.1.213
    Install-Package HuaweiCloud.SDK.Kafka -Version 3.1.213

Using the SDK

The following is a complete .NET example, which shows how to use the Huawei Cloud .NET SDK to call the ListInstances API.

  • To obtain the SDK example code for other APIs, see Obtaining SDK Sample Code.
  • This example provides only the basic API parameter settings. For more advanced settings, see the user manual of the SDK.
  • There will be huge security risks if the AK and SK used for authentication are directly written into the code. You are advised to encrypt the AK and SK and store them in the configuration file or set environment variables, and decrypt them when using them. In this example, the AK and SK are stored as the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK. For details, see Environment Variables.
using System;
using System.Collections.Generic;
using HuaweiCloud.SDK.Core;
using HuaweiCloud.SDK.Core.Auth;
using HuaweiCloud.SDK.Kafka;
using HuaweiCloud.SDK.Kafka.V2;
using HuaweiCloud.SDK.Kafka.V2.Model;
namespace ListInstancesSolution
{
    class Program
    {
        static void Main(string[] args)
        {
            // ==================== Initialize the client. ==================== 
            // Obtain the AK/SK from the environment variables. Before running, you need to set the AK/SK as environment variables.
            string ak = Environment.GetEnvironmentVariable("CLOUD_SDK_AK");
            string sk = Environment.GetEnvironmentVariable("CLOUD_SDK_SK");
            const string projectId = "195e********4a708ad9f9********1d";

            // Configure an authentication credential.
            var config = HttpConfig.GetDefaultConfig();
            config.IgnoreSslVerification = true;
            var auth = new BasicCredentials(ak, sk, projectId);

            // Initialize the client.
            var client = KafkaAsyncClient.NewBuilder()
                    .WithCredential(auth)
                    .WithRegion(KafkaRegion.ValueOf("ap-southeast-1"))
                    .WithHttpConfig(config)
                    .Build();

            // ==================== Construct a request object. ==================== 
            // Use the request object provided by the SDK to encapsulate request parameters.
            var req = new ListInstancesRequest
            {
                Engine = ListInstancesRequest.EngineEnum.FromValue("kafka")
            };

            // ==================== Initiate a request and handle the response. ==================== 
            // Use the asynchronous client to call the corresponding API. If the API is successfully called, a response object is returned. If the API fails to be called, an exception is thrown.
            try
            {
                var resp = await client.ListInstances(req);
                var respStatusCode = resp.HttpStatusCode;
                Console.WriteLine(respStatusCode);
            }
            catch (RequestTimeoutException requestTimeoutException)
            {
                Console.WriteLine(requestTimeoutException.ErrorMessage);
            }
            catch (ServiceResponseException clientRequestException)
            {
                Console.WriteLine(clientRequestException.HttpStatusCode);
                Console.WriteLine(clientRequestException.RequestId);
                Console.WriteLine(clientRequestException.ErrorCode);
                Console.WriteLine(clientRequestException.ErrorMsg);
            }
            catch (ConnectionException connectionException)
            {
                Console.WriteLine(connectionException.ErrorMessage);
            }
        }
    }
}