edit-icon download-icon

Use C# SDK

Last Updated: Mar 21, 2018

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 C# 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.

Set up credentials

When using the Alibaba Cloud C# SDK to access Alibaba Cloud services, you need an Alibaba Cloud account for authentication.

C# SDK supports the following authentication methods.

Authentications Descriptions
AccessKey Use the AccessKeyID/Secret to do the authentication
StsToken Use the STS Token to do the authentication
RamRoleArn Use the AssumeRole of the RAM account to do the authentication
EcsRamRole Use the RAM role of an ECS instance to do the authentication
RsaKeyPair Use the RSA key pair to do the authentication (supported only on Japan site)

This document uses AccessKey as an example to illustrate how to set up credentials.

Note: To ensure the security of your account, we recommend using your RAM account instead of the primary account. The primary account has full access to all of your cloud services, while the RAM account has limited access granted by the primary account to the cloud services.

Firstly, create an AccessKey as described in Create an AccessKey, and then set up your credentials when initializing AcsClient as follows:

Note: Do not disclose any code containing your AccessKey (do not commit the code to public GitHub projects). Otherwise, your Alibaba Cloud account may be compromised.

  1. IClientProfile clientProfile = DefaultProfile.GetProfile(
  2. "<your-region-id>",
  3. "<your-access-key-id>",
  4. "<your-access-key-secret>");

Initiate a call

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

  1. Create a client.

    1. DefaultAcsClient client = new DefaultAcsClient(clientProfile);
  2. Create a request.

    1. DescribeInstancesRequest request = new DescribeInstancesRequest();
    2. request.PageSize = 10;

    The naming convention for the request is ${apiName}Request. apiName is the action to perform, for example, DescribeInstances.

    If the API names are duplicate, the workspace name is used to differentiate the request.

  3. Send a request and handle the response.

    1. DescribeInstancesResponse response = client.GetAcsResponse(request);

    All the returned attributes are deserialized into the response. You can directly call response.xxx to obtain the response attributes.

    System.Console.WriteLine(response.TotalCount);

Thank you! We've received your feedback.