SDK integration

更新时间:
复制 MD 格式

To develop with Alibaba Cloud SDK V1.0 for .NET, you must integrate the Core SDK and the SDKs of individual Alibaba Cloud services as dependencies.

V1.0 SDK dependency information for cloud products

The V1.0 SDK for .NET of an Alibaba Cloud service provides request and response objects for API operations and an unmarshaller object for serializing return values. The following example shows how to add the Elastic Compute Service (ECS) SDK as a dependency:

.NET CLI

dotnet add package aliyun-net-sdk-ecs

# Specify the version using --version.
dotnet add package aliyun-net-sdk-ecs --version 4.24.18

Package Manager

Install-Package aliyun-net-sdk-ecs

# Specify the version using -version.
Install-Package aliyun-net-sdk-ecs -Version 4.24.18

PackageReference

<PackageReference Include="aliyun-net-sdk-ecs" Version="4.24.18" />

Paket CLI

paket add aliyun-net-sdk-ecs --version 4.24.18

F# Interactive

#r "nuget: aliyun-net-sdk-ecs, 4.24.18"
Note

The V1.0 SDK uses the naming format aliyun-net-sdk-${Product name}. For more information about the V1.0 SDK for a specific cloud product, see SDK Center.

Core SDK dependencies

The Core SDK handles HTTP requests to OpenAPI, authentication, signature algorithms, and exception handling. Add the dependency as follows:

dotnet add package aliyun-net-sdk-core

Use the SDK

1. Initialize a client

In SDK V1.0 for .NET, all Alibaba Cloud service SDKs share the same core library. You can use the core library to initialize an SDK client that processes API requests. The following example shows how to initialize a client. For more information about initialization methods, see Manage access credentials.

static void Main(string[] args)
{
    IClientProfile profile = DefaultProfile.GetProfile(
        // The region ID.
        "<REGION_ID>",
        // Obtain the AccessKey ID of the RAM user from an environment variable.
        Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
        // Obtain the AccessKey secret of the RAM user from an environment variable.
        Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    DefaultAcsClient client = new DefaultAcsClient(profile);
}
Note

If a single SDK client handles multiple threads, security issues may occur. If multiple Alibaba Cloud services share the same profile, unauthorized access may occur. We recommend that you use SDK V2.0 for .NET instead.

2. Construct a request object and initiate a request

SDK V1.0 for .NET sends API requests through the core library. You pass request parameters using the request object from the service SDK, then call the initialized client to send the request and receive a response object. The following example uses the ECS SDK to query ECS instance information.

Note

Each API operation has its own request and response objects. They are named in the following formats:

  • ${API}${Request}. For example, a request object can be DescribeInstancesRequest.

  • ${API}${Response}. For example, a response object can be DescribeInstancesResponse.

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Ecs.Model.V20140526;

namespace AlibabaCloud.SDK.Sample
{
    public class Sample 
    {
        static void Main(string[] args)
        {
            IClientProfile profile = DefaultProfile.GetProfile(
                // The region ID.
                "<REGION_ID>",
                // Obtain the AccessKey ID of the RAM user from an environment variable.
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                // Obtain the AccessKey secret of the RAM user from an environment variable.
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            DefaultAcsClient client = new DefaultAcsClient(profile);
            // Create a request object.
            DescribeInstancesRequest request = new DescribeInstancesRequest{
                RegionId = "<REGION_ID>"
            };;
            // Send the request from the client and serialize the result into a Response object.
            DescribeInstancesResponse response = client.GetAcsResponse(request);
            System.Console.WriteLine(response.TotalCount);
        }
    }
}