Set up and run your first Alibaba Cloud SDK for .NET project in VS Code on Windows.
Prerequisites
-
.NET is installed. Install .NET on Windows.
-
VS Code is installed. Set up a .NET development environment on Windows.
Use the SDK
Use a sample project from OpenAPI Explorer
If the sample project is unavailable, Install the SDK in an existing project.
-
In OpenAPI Explorer, search for the API operation to call, such as ECS DescribeRegions, and click the API name to open the debugging page.

-
Configure the API parameters on the parameters tab. Review the documentation tab for operation details and billing information.

-
On the SDK Sample Code tab, download and extract the SDK project.

-
In VS Code, open the extracted project folder.
-
Obtain a RAM user AccessKey pair. Create an AccessKey for a RAM user.
ImportantConfigure the AccessKey pair as environment variables. Configure environment variables on Linux, macOS, and Windows systems.
-
Run the project. A response without exceptions indicates success.
cd core dotnet run
Install the SDK in an existing project
-
In VS Code, open a .NET project folder. Run
dotnet new console -n V2SDKProjectto create a console project, then runcd V2SDKProjectto enter it. -
Install the SDK. In SDK Center, select a cloud product (ECS in this example), set SDK Version to V2.0 and the language to C#, then copy and run the installation command.
dotnet add package AlibabaCloud.SDK.Ecs20140526 --version 4.1.9 -
Initialize the ECS client.
ImportantYou must use an AccessKey pair to complete identity verification when you initialize the client. In this case, you must obtain an AccessKey pair in advance. For more information about how to obtain an AccessKey pair, see Create an AccessKey.
After you obtain the AccessKey pair of a RAM user, you must configure the AccessKey pair in environment variables. For more information, see Configure environment variables in Linux, macOS, and Windows.
For more information about how to configure the endpoint, see Endpoints.
public static AlibabaCloud.SDK.Ecs20140526.Client CreateClient() { AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config { // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set. AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set. AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), }; // For more information about endpoints, see https://api.aliyun.com/product/Ecs config.Endpoint = "ecs.cn-shanghai.aliyuncs.com"; return new AlibabaCloud.SDK.Ecs20140526.Client(config); } -
Call the API. Review the API Documentation for the target API. The following example calls the `DescribeRegions` API of ECS.
NoteEach API has a separate request object that follows the `${APIName}${Request}` naming convention, such as `DescribeRegionsRequest`.
AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest describeRegionsRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest { AcceptLanguage = "en-US", }; -
Add error handling. Exception handling.
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Tea; using Tea.Utils; namespace AlibabaCloud.SDK.Sample { public class Sample { /** * Use an AccessKey ID and AccessKey secret to initialize the client. * @return Client * @throws Exception */ public static AlibabaCloud.SDK.Ecs20140526.Client CreateClient() { AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config { // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set. AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set. AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), }; config.Endpoint = "ecs.cn-shanghai.aliyuncs.com"; return new AlibabaCloud.SDK.Ecs20140526.Client(config); } public static void Main(string[] args) { AlibabaCloud.SDK.Ecs20140526.Client client = CreateClient(); AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest describeRegionsRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest { AcceptLanguage = "en-US", }; AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions(); try { AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsResponse resp = client.DescribeRegionsWithOptions(describeRegionsRequest, runtime); Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(resp)); } catch (TeaException error) { // This is for demonstration only. Handle exceptions with caution and do not ignore them in your project. // Error message Console.WriteLine(error.Message); // Diagnosis URL Console.WriteLine(error.Data["Recommend"]); AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message); } catch (Exception _error) { TeaException error = new TeaException(new Dictionary<string, object> { { "message", _error.Message } }); // This is for demonstration only. Handle exceptions with caution and do not ignore them in your project. // Error message Console.WriteLine(error.Message); // Diagnosis URL Console.WriteLine(error.Data["Recommend"]); AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message); } } } } -
Run the sample code.
dotnet run