This topic describes how to use Alibaba Cloud SDKs for .NET in an Integrated Development Environment (IDE) on Windows. In this example, Visual Studio (VS) Code is used.
Prerequisites
.NET is installed. For more information, see Install .NET on Windows.
VS Code is installed. For more information, see Build a .NET development environment on Windows.
Use an SDK
Use a sample project provided in OpenAPI Explorer
You may fail to download a sample project for a specific API operation. In this case, you can use an SDK in an existing project. For more information, see the Install an SDK in an existing project section of this topic.
Go to OpenAPI Explorer. Search for the API operation that you want to call. In this example, the DescribeRegions operation of Elastic Compute Service (ECS) is used. Enter DescribeRegions in the search box and click the operation name in the search results to go to the API debugging page. |

On the Parameters tab, specify the parameters based on your requirements. When you specify the parameters, read the information on the Document tab on the right side of the debugging page. Make sure that you understand the usage notes of the operation and the description of each parameter. Pay attention to billing-related information.

On the SDK Sample Code tab on the right side of the debugging page, select the C# programming language and click Download Project to download the complete sample project package to your computer. Then, decompress the package.

Open VS Code, click Open Folder, and then select the decompressed project folder.
Before you call this operation, you must obtain an AccessKey pair as the access credential. We recommend that you use the AccessKey pair of a Resource Access Management (RAM) user. For more information, see the "Create an AccessKey pair for a RAM user" section of the Create an AccessKey pair topic.
ImportantAfter 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.
After you configure the AccessKey pair in environment variables, run the following commands to run the project. If no exception is thrown, the API call is successful.
cd core dotnet run
Install an SDK in an existing project
Create an app project. Open VS Code. In the top navigation bar, choose File > Open Folder, and then select a project folder. Run the
dotnet new console -n V2SDKProjectcommand in the terminal to create a console app project. Then, run thecd V2SDKProjectcommand to go to the directory of the project.Install the SDK. Visit the SDK Center and select the cloud service that you want to manage. In this example, ECS is used. On the page that appears, select V2.0 from the SDK Generation drop-down list and C# in the All languages section. Copy the command that is used to install the SDK and run the command in the terminal.
dotnet add package AlibabaCloud.SDK.Ecs20140526 --version 4.1.9Initialize the client. If you want to call the ECS API, you must initialize an 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 pair.
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 configured in the code runtime environment. AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment. AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), }; config.Endpoint = "ecs.cn-shanghai.aliyuncs.com"; return new AlibabaCloud.SDK.Ecs20140526.Client(config); }Call the API operation. Before you call an API operation, you must read the corresponding API documentation. In this example, the DescribeRegions operation of ECS is used.
NoteEach API operation has a request object, named in the ${API name}${Request} format. Example: DescribeRegionsRequest.
AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest describeRegionsRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest { AcceptLanguage = "zh-CN", };Handle exceptions. Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. We recommend that you take reasonable measures to handle exceptions, such as propagating exceptions in a proper manner, recording logs, and recovering from exceptions. This helps ensure the robustness and stability of the system. For more information about how to handle exceptions in V2.0 SDKs for .NET, see Exception handling.
// This file is auto-generated, don't edit it. Thanks. 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 your AccessKey ID and AccessKey secret to initialize a 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 configured in the code runtime environment. AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment. 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 = "zh-CN", }; 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) { // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed in the console. // The error message. Console.WriteLine(error.Message); // The URL of the corresponding error diagnostics page. 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 } }); // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed in the console. // The error message. Console.WriteLine(error.Message); // The URL of the corresponding error diagnostics page. Console.WriteLine(error.Data["Recommend"]); AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message); } } } }Run the sample code.
dotnet run