All Products
Search
Document Center

Alibaba Cloud SDK:Integrate Alibaba Cloud SDK V1.0 for .NET

Last Updated:Dec 13, 2024

When you use Alibaba Cloud SDK V1.0 for .NET for development, you must correctly integrate the Core SDK and SDKs of Alibaba Cloud services as dependencies. This topic describes how to configure and manage these dependencies.

Configure the SDK V1.0 for .NET of an Alibaba Cloud service as a dependency

The SDK V1.0 for .NET of an Alibaba Cloud service provides the request and response objects of API operations and the unmarshaller object that is used to serialize the values to be returned. The following sample code shows how to configure the SDK V1.0 for .NET of Elastic Compute Service (ECS) as a dependency:

.NET CLI

dotnet add package aliyun-net-sdk-ecs

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

Package Manager

Install-Package aliyun-net-sdk-ecs

# Specify the version by 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 SDK V1.0 for .NET of an Alibaba Cloud service is named in the aliyun-net-sdk-${Service name} format. You can also go to SDK Center to query information about the SDK V1.0 for a specific cloud service.

Configure the Core SDK as a dependency

The Core SDK contains the logic for HTTP-based API requests, authentication information, signature algorithms, and exception handling. The following sample code shows how to configure the Core SDK as a dependency:

dotnet add package aliyun-net-sdk-core

Use the SDK

1. Initialize a client

In SDK V1.0 for .NET, SDKs of all Alibaba Cloud services share the same core library. Therefore, you can use the method provided in the core library to initialize an SDK client to process API requests for accessing Alibaba Cloud services. The following sample code shows how to initialize a client of the SDK V1.0 for .NET. For more information about client initialization methods, see Manage access credentials.

static void Main(string[] args)
{
    IClientProfile profile = DefaultProfile.GetProfile(
        // Specify the region ID.
        "<REGION_ID>",
        // Obtain the AccessKey ID of the Resource Access Management (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 one SDK client processes multiple threads, security issues may occur. If multiple Alibaba Cloud services share the same profile information, unauthorized access may occur. We recommend that you use SDK V2.0 for .NET for development.

2. Construct a request object and initiate a request

SDK V1.0 for .NET initiates API requests by using the core library. You can use the request object provided by an SDK of the cloud service to pass request parameters, and then initiate a request by using the initialized client. This way, a response object is returned that is used to receive the response parameters. The following sample code shows how to use the SDK V1.0 for .NET of ECS to call API operations of ECS to query information about one or more ECS instances.

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(
                // Specify 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);
            // Construct the request object.
            DescribeInstancesRequest request = new DescribeInstancesRequest{
                RegionId = "<REGION_ID>"
            };;
            // Initiate a request from the client and serialize the returned values into a response object.
            DescribeInstancesResponse response = client.GetAcsResponse(request);
            System.Console.WriteLine(response.TotalCount);
        }
    }
}