English | 简体中文
The Huawei Cloud .Net SDK allows you to easily work with Huawei Cloud services such as Elastic Compute Service (ECS) and Virtual Private Cloud(VPC) without the need to handle API related tasks.
This document introduces how to obtain and use Huawei Cloud .Net SDK.
-
To use Huawei Cloud .Net SDK, you must have Huawei Cloud account as well as the Access Key and Secret key of the Huawei Cloud account. You can create an Access Key in the Huawei Cloud console. For more information, see My Credentials.
-
To use Huawei Cloud .Net SDK to access the APIs of specific service, please make sure you do have activated the service in Huawei Cloud console if needed.
-
The .NET SDK requires:
- .NET Standard 2.0 or above
- C# 4.0 or above
Run the following commands to install .Net SDK:
You must install HuaweiCloud.SDK.Core
library no matter which product development kit you need to use. Take using VPC
SDK for example, you need to install HuaweiCloud.SDK.Core
library and HuaweiCloud.SDK.Vpc
library:
- Use .NET CLI
dotnet add package HuaweiCloud.SDK.Core
dotnet add package HuaweiCloud.SDK.Vpc
- Use Package Manager
Install-Package HuaweiCloud.SDK.Core
Install-Package HuaweiCloud.SDK.Vpc
- The following example shows how to query a list of VPC in a specific region, you need to substitute your
real
{Service}Client
forVpcClient
in actual use. - Substitute the values for
{your ak string}
,{your sk string}
,{your endpoint string}
and{your project id}
.
using System;
using HuaweiCloud.SDK.Core;
using HuaweiCloud.SDK.Core.Auth;
using HuaweiCloud.SDK.Vpc.V2;
using HuaweiCloud.SDK.Vpc.V2.Model;
using Microsoft.Extensions.Logging;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
const string ak = "{your ak string}";
const string sk = "{your sk string}";
const string endpoint = "{your endpoint string}";
const string projectId = "{your projectID string}";
Credentials auth = new BasicCredentials(ak, sk, projectId);
var config = HttpConfig.GetDefaultConfig();
config.IgnoreSslVerification = true;
VpcClient vpcClient = VpcClient.NewBuilder()
.WithCredential(auth)
.WithEndPoint(endpoint)
.WithHttpConfig(config)
.WithLogging(LogLevel.Information)
.Build();
var request = new ListVpcsRequest
{
Limit = 1
};
try
{
var response = vpcClient.ListVpcs(request);
Console.WriteLine(JsonUtils.Serialize(response.Vpcs));
}
catch (RequestTimeoutException requestTimeoutException)
{
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException serviceResponseException)
{
Console.WriteLine(serviceResponseException.HttpStatusCode);
Console.WriteLine(serviceResponseException.ErrorCode);
Console.WriteLine(serviceResponseException.ErrorMsg);
}
catch (ConnectionException connectionException)
{
Console.WriteLine(connectionException.ErrorMessage);
}
}
}
}
Detailed changes for each released version are documented in the CHANGELOG.md.
User Manual 🔝
- 1. Client Configuration
- 2. Credentials Configuration
- 3. Client Initialization
- 4. Send Requests and Handle Responses
- 5. Use Asynchronous Client
- 6. Troubleshooting
- 7. FAQ
1. Client Configuration 🔝
1.1 Default Configuration 🔝
// Use default configuration
var config = HttpConfig.GetDefaultConfig();
1.2 Network Proxy 🔝
Use network proxy if needed.
- Only HTTP proxy is supported if you have assigned proxy port when configuring proxy.
config.ProxyHost = "proxy.huaweicloud.com";
// assign proxy port
config.ProxyPort = 8080;
config.ProxyUsername = "test";
config.ProxyPassword = "test";
- Both HTTP and HTTPS proxy are supported if proxy port is unassigned when configuring proxy.
config.ProxyHost = "https://proxy.huaweicloud.com:8080";
config.ProxyUsername = "test";
config.ProxyPassword = "test";
1.3 Connection 🔝
config.Timeout = 120;
1.4 SSL Certification 🔝
// Skip ssl certifaction checking while using https protocal if needed
config.IgnoreSslVerification = true;
2. Credentials Configuration 🔝
There are two types of Huawei Cloud services, regional
services and global
services.
Global services contain IAM, TMS, EPS.
For regional
services' authentication, projectId is required to initialize BasicCredentials. For global
services'
authentication, domainId is required to initialize GlobalCredentials.
Parameter description:
ak
is the access key ID for your account.sk
is the secret access key for your account.projectId
is the ID of your project depending on the region you want to operate.domainId
is the account ID of Huawei Cloud.securityToken
is the security token when using temporary AK/SK.
You could use permanent AK and SK or use temporary AK and SK and SecurityToken to complete credentials' configuration.
2.1 Use Permanent AK&SK 🔝
// Regional services
Credentials basicCredentials = new BasicCredentials(ak, sk, projectId);
// Global services
Credentials globalCredentials = new GlobalCredentials(ak, sk, domainId);
Notice:
- projectId/domainId supports automatic acquisition in version
3.0.26-beta
or later, if you want to use this feature, you need to provide the ak and sk of your account and the id of the region, and then build your client instance with methodWithRegion()
, detailed example could refer to 3.2 Initialize the client with specified Region .
2.2 Use Temporary AK&SK 🔝
It's required to obtain temporary access key, security key and security token first, which could be obtained through permanent access key and security key or through an agency.
Obtaining a temporary access key token through permanent access key and security key, you could refer to
document: https://support.huaweicloud.com/en-us/api-iam/iam_04_0002.html . The API mentioned in the document above
corresponds to the method of CreateTemporaryAccessKeyByToken
in IAM SDK.
Obtaining a temporary access key and security token through an agency, you could refer to
document: https://support.huaweicloud.com/en-us/api-iam/iam_04_0101.html . The API mentioned in the document above
corresponds to the method of CreateTemporaryAccessKeyByAgency
in IAM SDK.
// Regional services
Credentials basicCredentials = new BasicCredentials(ak, sk, projectId).WithSecurityToken(securityToken);
// Global services
Credentials globalCredentials = new GlobalCredentials(ak, sk, domainId).WithSecurityToken(securityToken);
3. Client Initialization 🔝
There are two ways to initialize the {Service}Client, you could choose one you preferred.
3.1 Initialize the {Service}Client with specified Endpoint 🔝
// Specify the endpoint, take the endpoint of VPC service in region of cn-north-4 for example
String endpoint = "https://vpc.cn-north-4.myhuaweicloud.com";
// Initialize the credentials, you should provide projectId or domainId in this way, take initializing BasicCredentials for example
Credentials basicCredentials = new BasicCredentials(ak, sk, projectId);
// Initialize specified {Service}Client instance, take initializing the regional service VPC's VpcClient for example
VpcClient vpcClient = VpcClient.NewBuilder()
.WithCredential(basicCredentials)
.WithEndPoint(endpoint)
.WithHttpConfig(config)
.Build();
where:
-
endpoint
is the service specific endpoints, see Regions and Endpoints. -
When you meet some trouble in getting projectId using the specified region way, you could use this way instead.
3.2 Initialize the {Service}Client with specified Region (Recommended) 🔝
// Initialize the credentials, projectId or domainId could be unassigned in this situation, take initializing GlobalCredentials for example
Credentials globalCredentials = new GlobalCredentials(ak, sk);
// Initialize specified {Service}Client instance, take initializing the global service IAM's IamClient for example
IamClient iamClient = IamClient.NewBuilder()
.WithCredential(globalCredentials)
.WithRegion(IamRegion.CN_NORTH_4)
.WithHttpConfig(config)
.Build();
Notice:
-
If you use {Service}Region to initialize {Service}Client, projectId/domainId supports automatic acquisition, you don't need to configure it when initializing Credentials.
-
Multiple ProjectId situation is not supported.
-
Supported region list: af-south-1, ap-southeast-1, ap-southeast-2, ap-southeast-3, cn-east-2, cn-east-3, cn-north-1, cn-north-4, cn-south-1, cn-southwest-2, ru-northwest-2. You may get exception such as
Unsupported regionId
if your region don't in the list above.
Comparison of the two ways:
Initialization | Advantages | Disadvantage |
---|---|---|
Specified Endpoint | The API can be invoked successfully once it has been published in the environment. | You need to prepare projectId and endpoint yourself. |
Specified Region | No need for projectId and endpoint, it supports automatic acquisition if you configure it in the right way. | The supported services and regions are limited. |
4. Send Requests and Handle Responses 🔝
// Send request and print response, take interface of ListVpcs for example
var request = new ListVpcsRequest
{
Limit = 1,
};
var response = vpcClient.ListVpcs(request)
Console.WriteLine(JsonUtils.Serialize(response.Vpcs));
4.1 Exceptions 🔝
Level 1 | Notice | Level 2 | Notice |
---|---|---|---|
ConnectionException | Connection error | HostUnreachableException | Host is not reachable |
SslHandShakeException | SSL certification error | ||
RequestTimeoutException | Request timeout | CallTimeoutException | timeout for single request |
RetryOutageException | no response after retrying | ||
ServiceResponseException | service response error | ServerResponseException | server inner error, http status code: [500,] |
ClientRequestException | invalid request, http status code: [400, 500) |
// Handle ClientRequestExceptions
try
{
var request = new ListVpcsRequest
{
Limit = 1,
};
var response = vpcClient.ListVpcs(request);
Console.WriteLine(JsonUtils.Serialize(response.Vpcs));
}
catch (ServiceResponseException serviceResponseException)
{
Console.WriteLine(serviceResponseException.HttpStatusCode);
Console.WriteLine(serviceResponseException.ErrorCode);
Console.WriteLine(serviceResponseException.ErrorMsg);
Console.WriteLine(serviceResponseException.RequestId);
}
5. Use Asynchronous Client 🔝
// Initialize asynchronous client instance, take VpcAsyncClient for example
var vpcClient = VpcAsyncClient.NewBuilder()
.WithCredential(auth)
.WithEndPoint(endpoint)
.WithHttpConfig(config)
.Build();
// send asynchronous request
var future = vpcClient.ListVpcsAsync(new ListVpcsRequest()
{
Limit = 1
});
// get asynchronous response
var response = future.Result;
Console.WriteLine(JsonUtils.Serialize(response.Vpcs));
6. Troubleshooting 🔝
SDK supports Access
log and Debug
log which could be configured manually.
6.1 Access Log 🔝
SDK supports print access log which could be enabled by manual configuration, the log could be output to the console.
For example:
var vpcClient = VpcClient.NewBuilder()
.WithCredential(auth)
.WithEndPoint(endpoint)
// configure log level and request will be print on the console
.WithLogging(LogLevel.Information)
.WithHttpConfig(config)
.Build();
After enabled log, the SDK will print the access log by default, every request will be recorded to the console like:
info: System.Net.Http.HttpClient.SdkHttpClient.LogicalHandler[100]
Start processing HTTP request GET https://vpc.cn-north-1.myhuaweicloud.com/v1/076958154900d2492f8bc0197405c803/vpcs?limit=1
info: System.Net.Http.HttpClient.SdkHttpClient.ClientHandler[100]
Sending HTTP request GET https://vpc.cn-north-1.myhuaweicloud.com/v1/076958154900d2492f8bc0197405c803/vpcs?limit=1
info: System.Net.Http.HttpClient.SdkHttpClient.ClientHandler[101]
Received HTTP response after 517.5326ms - OK
info: System.Net.Http.HttpClient.SdkHttpClient.LogicalHandler[101]
End processing HTTP request after 543.6428ms - OK
6.2 Original HTTP Listener 🔝
In some situation, you may need to debug your http requests, original http request and response information will be needed. The SDK provides a listener function to obtain the original encrypted http request and response information.
⚠️ Warning: The original http log information is used in debugging stage only, please do not print the original http header or body in the production environment. These log information is not encrypted and contains sensitive data such as the password of your ECS virtual machine, or the password of your IAM user account, etc. When the response body is binary content, the body will be printed as "***" without detailed information.
private void RequestHandler(HttpRequestMessage message, ILogger logger)
{
logger.LogDebug(message.ToString());
}
private void ResponseHandler(HttpResponseMessage message, ILogger logger)
{
logger.LogDebug(message.ToString());
}
var vpcClient = VpcClient.NewBuilder()
.WithCredential(auth)
.WithEndPoint(endpoint)
.WithLogging(LogLevel.Debug)
.WithHttpConfig(config)
.WithHttpHandler(new HttpHandler()
.AddRequestHandler(RequestHandler)
.AddResponseHandler(ResponseHandler))
.Build();
where:
HttpHandler supports method AddRequestHandler
and AddResponseHandler
.
7. FAQ 🔝
Use .Net Framework 4.7 to integrate .Net SDK, a dead lock occurs
[Symptom]: When using synchronized client to call an interface, and the program has been started, but where is no error message or timeout occurs.
[Cause]: The inner implementation of sending requests in synchronized client of SDK is to use an asynchronous task, and SDK will await this task. In such scenario, deadlock occurs between the context of the .Net Framework UI and the asynchronous task context of the SDK. As a result, the asynchronous task of the SDK cannot be activated. Original article
[Solution]: Switch the synchronous client to the asynchronous client. If the UI events and API requests are both asynchronous, there will be no deadlock.