edit-icon download-icon

Getting started

Last Updated: Mar 21, 2018

The Alibaba Cloud C# Software Development Kit (SDK) allows you to access Alibaba Cloud services such as Elastic Compute Service (ECS), Server Load Balancer (SLB), and CloudMonitor. You can access Alibaba Cloud services without the need to handle API related tasks, such as signing and constructing your requests.

This document introduces how to install and use Alibaba Cloud C# SDK.

If you have any problem while using Java SDK, please join the DingTalk group: 11771185 (the official SDK customer service group of Alibaba Cloud) for consultation.

Prerequisites

  • To use Alibaba Cloud C# SDK, you must have an Alibaba Cloud account and an AccessKey.

    The AccessKey is required when initializing the client. You can create an AccessKey in the Alibaba Cloud console. For more information, see Create an AccessKey.

    Note: To increase the security of your account, we recommend that you use the AccessKey of the RAM user to access Alibaba Cloud services.

  • To use Alibaba Cloud Java SDK to access the APIs of a product, you must first activate the product on the Alibaba Cloud console if required.

  • The Alibaba Cloud C# SDK is requires .NET Framework 4.0 or later.

Install C# SDK

You must install the SDK core library for any SDK you use. For example, to call the ECS SDK, you must install both the ECS SDK and the SDK core library.

Install the Alibaba Cloud C# SDK using one of the following methods:

  • Add DLL reference

    1. Download the DLL package from .NET SDK.

    2. Right click your project in the Solution Explorer of Visual Studio and click Reference.

    3. In the displayed menu, click Add Reference.

    4. In the displayed dialog box, click Browse. Then select the downloaded DLL file and click Confirm.

  • Add project reference

    1. Run the following command to clone the SDK source codes from GitHub.

      1. git clone https://github.com/aliyun/aliyun-openapi-net-sdk.git

      There are many folders prefixed withaliyun-net-openapi-in the cloned directory. Each folder contains \\*.csproj file, which is the project file**. For example, there is an aliyun-net-sdk-ecs.csproj file under the aliyun-net-openapi-ecs subfolder.

    2. In Visual Studio, right click your solution.

    3. Click Add > Existing Project.

    4. In the displayed dialogue box, select the project file, for example, aliyun-net-sdk-ecs.csproj, and click then Open.

    5. Right click your project and click Reference > Add Reference.

    6. In the displayed dialog box, click the Project tab, select the opened project and click Confirm.

Initiate a call

The following code example shows the three main steps to use the Alibaba Cloud C# SDK:

  • Create and initialize a DefaultAcsClient instance.

  • Create a request and set parameters.

  • Initiate the request and handle the response.

  1. using Aliyun.Acs.Core;
  2. using Aliyun.Acs.Core.Profile;
  3. using Aliyun.Acs.Core.Exceptions;
  4. using Aliyun.Acs.Ecs.Model.V20140526;
  5. class TestProgram
  6. {
  7. static void Main(string[] args)
  8. {
  9. // Create a client used for initiating a request
  10. IClientProfile profile = DefaultProfile.GetProfile(
  11. "<your-region-id>",
  12. "<your-access-key-id>",
  13. "<your-access-key-secret>");
  14. DefaultAcsClient client = new DefaultAcsClient(profile);
  15. try
  16. {
  17. // Create the request
  18. DescribeInstancesRequest request = new DescribeInstancesRequest();
  19. request.PageSize = 10;
  20. // Initiate the request and get the response
  21. DescribeInstancesResponse response = client.GetAcsResponse(request);
  22. System.Console.WriteLine(response.TotalCount);
  23. }
  24. catch (ServerException ex)
  25. {
  26. System.Console.WriteLine(ex.ToString());
  27. }
  28. catch (ClientException ex)
  29. {
  30. System.Console.WriteLine(ex.ToString());
  31. }
  32. }
  33. }
Thank you! We've received your feedback.