All Products
Search
Document Center

Alibaba Cloud SDK:Generic calls

Last Updated:Jun 16, 2025

Alibaba Cloud SDK V1.0 for .NET supports generic API calls. This topic describes how to make generic calls by using Alibaba Cloud SDK V1.0 for .NET.

Characteristics

  • Lightweight: You need only to download the core package. You do not need to download and install SDKs of Alibaba Cloud services.

  • High compatibility: If a cloud service does not provide an SDK, or the SDK is not updated for the latest API operations, you can make generic calls. This way, you can call the latest API operations without the need to wait for SDK updates.

For more information, see Generic calls and specialized calls.

Usage notes

Before you make a generic call, manually obtain and specify the required metadata, including the API version, request URL, and parameter type. For more information, see API metadata.

Install the core library of Alibaba Cloud SDK

Run the following command on the terminal to install the core library of Alibaba Cloud SDK V1.0 for .NET. For more information about the latest version of the core library, see aliyun-net-sdk-core.

dotnet add package aliyun-net-sdk-core

Call an API operation

Initialize a request client

Create a DefaultAcsClient object and initialize the request client. In this example, an AccessKey pair is used to initialize the request client. For more information about other initialization methods, see Manage access credentials.

Note

To prevent AccessKey leaks, you can record the AccessKey pair in environment variables. For more information, see Configure environment variables in Linux, macOS, and Windows.

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;

namespace AlibabaCloud.SDK.Sample
{
    public class Sample
    {
        public 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);
        }
    }
}

Configure the API operation information and request parameters

Use CommonRequest to configure the common request parameters and operation-specific parameters for the API operation. For more information about the common request parameters, see Advanced settings.

Operation-specific parameters

How a request parameter is passed is determined by the metadata of the API operation. For example, the DescribeInstanceStatus API operation is defined as {"name":"RegionId","in":"query",...}} in the metadata. In this case, "in":"query" indicates that the region ID (RegionId) must be passed in AddQueryParameters.

Description

How the parameter is passed

"in":"query"

AddQueryParameters(string key,string value)

Note

If the request parameter specifies a collection, pass the request parameter in the following format: AddQueryParameters("key.1","value1") and AddQueryParameters("key.2","value2").

"in":"body" or "in": "formData"

AddBodyParameters(string key,string value)

Note

If the request parameter does not specify a string, convert the parameter value to a JSON string and specify the string as the variable value.

Upload files

SetContent(byte[] content,string charset,FormatType formatType)

Note

Set formatType to FormatType.RAW.

       // 2. Create an API request and configure the request parameters.
        CommonRequest request = new CommonRequest();
        // 2.1  Configure the common request parameters.
        request.Domain = "ecs-cn-hangzhou.aliyuncs.com"; // The endpoint of the service.
        request.Version = "2014-05-26"; // The API version of the service.
        request.Action = "DescribeInstanceStatus"; // The name of the API operation. The operation name is required for remote procedure call (RPC) API operations and optional for resource-oriented architecture (ROA) API operations.
        request.Method = MethodType.POST; // The request method. Value values: MethodType.POST, MethodType.GET, MethodType.PUT, MethodType.DELETE, MethodType.HEAD, and MethodType.OPTIONS.
        request.Protocol = ProtocolType.HTTPS; // The request protocol. Valid values: ProtocolType.HTTPS and ProtocolType.HTTP.
        request.TimeoutInMilliSeconds = 1000; // The timeout period.
        // request.UriPattern = "/";  // The resource path, which is required by ROA-style API operations. Do not configure this parameter for RPC-style API operations. 

        // 2.2 Configure the operation-specific parameters.
        // Scenario 1: Specify the query parameters in AddQueryParameters(string key,string value).
        request.AddQueryParameters("RegionId", "cn-hangzhou");
        List<string> instanceIds = new List<string> { "i-bp124uve8zq7XXXXXXXX", "i-bp1axhql4dqaXXXXXXXX" };
        for (int i = 0; i < instanceIds.Count; i++) {
		request.AddQueryParameters($"InstanceId.{i + 1}" , instanceIds[i]);
	    }
        request.AddQueryParameters("PageNumber", "1");
        request.AddQueryParameters("PageSize", "30");

        // Scenario 2: Specify the body parameters in AddBodyParameters(string key,string value).
        // request.AddBodyParameters("key1", "value1");
        // request.AddBodyParameters("key2", "value2");

        // Scenario 3: To upload files, specify SetContent(byte[] content,string charset,FormatType formatType). Set formatType to FormatType.RAW. 
        // byte[] content = File.ReadAllBytes(@"<FILE_PATH>");
        // request.SetContent(content, "UTF-8",FormatType.RAW); 
        

Initiate a request

Use the client created in the preceding step and call the GetCommonResponse method to initiate a request.

CommonResponse response = client.GetCommonResponse(request);
System.Console.WriteLine(response.Data);

Example: Call an RPC-style API operation

The following sample code provides an example on how to use CommonRequest to call the DescribeInstanceStatus operation of ECS:

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
class Sample
{
    static void Main(string[] args)
    {
        // Create a client instance to initiate a request.
        IClientProfile profile = DefaultProfile.GetProfile(
            // Specify the region ID.
            "cn-hangzhou",
            // Obtain the AccessKey ID of a RAM user from environment variables.
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            // Obtain the AccessKey SECRET of a RAM user from environment variables.
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        DefaultAcsClient client = new DefaultAcsClient(profile);
        try
        {
            // Construct a request.
            CommonRequest request = new CommonRequest();
            request.Domain = "ecs.aliyuncs.com";
            request.Version = "2014-05-26";
            // Specify the value of the ApiName (Action) parameter for the RPC API operation.
            request.Action = "DescribeInstanceStatus";
            request.AddQueryParameters("RegionId", "cn-hangzhou");
            List<string> instanceIds = new List<string> { "i-bp124uve8zq7XXXXXXXX", "i-bp1axhql4dqaXXXXXXXX" };
            for (int i = 0; i < instanceIds.Count; i++) {
                    request.AddQueryParameters($"InstanceId.{i + 1}" , instanceIds[i]);
                }
            request.AddQueryParameters("PageNumber", "1");
            request.AddQueryParameters("PageSize", "30");
            // Initiate the request and obtain the response.
            CommonResponse response = client.GetCommonResponse(request);
            System.Console.WriteLine(response.Data);
        }
        catch (ServerException ex)
        {
            System.Console.WriteLine(ex.ToString());
        }
        catch (ClientException ex)
        {
            System.Console.WriteLine(ex.ToString());
        }
    }
}

Example: Call a RESTful-style (ROA-style) API operation

The following sample code provides an example on how to use CommonRequest to call an API operation of ACK to query all clusters:

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
class Sample
{
    static void Main(string[] args)
    {
        // Create a client instance to initiate a request.
        IClientProfile profile = DefaultProfile.GetProfile(
            // Specify the region ID.
            "<REGION-ID>",
            // Obtain the AccessKey ID of a RAM user from environment variables.
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            // Obtain the AccessKey SECRET of a RAM user from environment variables.
            Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        DefaultAcsClient client = new DefaultAcsClient(profile);
        try
        {
            // Construct a request.
            CommonRequest request = new CommonRequest();
            request.Domain = "cs.aliyuncs.com";
            request.Version = "2015-12-15";
            // Specify the value of the UriPattern parameter for the Restful API operation.
            request.UriPattern = "/clusters";
            // Initiate the request and obtain the response.
            CommonResponse response = client.GetCommonResponse(request);
            System.Console.WriteLine(response.Data);
        }
        catch (ServerException ex)
        {
            System.Console.WriteLine(ex.ToString());
        }
        catch (ClientException ex)
        {
            System.Console.WriteLine(ex.ToString());
        }
    }
}

FAQ

  1. What do I do if the "The input parameter \"AccessKeyId\" that is mandatory for processing this request is not supplied." error message is returned?

    Cause: The AccessKey pair is not correctly configured.

    Solutions:

    1. Run the following commands to check whether the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured.

      Linux/macOS

      echo $ALIBABA_CLOUD_ACCESS_KEY_ID
      echo $ALIBABA_CLOUD_ACCESS_KEY_SECRET

      Windows

      echo %ALIBABA_CLOUD_ACCESS_KEY_ID%
      echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET%

      If a valid AccessKey pair is returned, the environment variables are properly configured. If no AccessKey pair or an invalid AccessKey pair is returned, configure the environment variables as required. For more information, see Configure environment variables in Linux, macOS, and Windows.

    2. Check for errors related to the AccessKey pair in the code.

      Sample error request:

      AccessKeyId = Environment.GetEnvironmentVariable("yourAccessKeyID"),
      AccessKeySecret = Environment.GetEnvironmentVariable("yourAccessKeySecret"),

      Sample success request:

      Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), 
      Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
  2. What do I do if the "Unhandled exception. Aliyun.Acs.Core.Exceptions.ClientException: SDK.WebException : HttpWebRequest WebException occured, the request url is XXX.cn-hangzhou.aliyuncs.com System.Net.WebException: An eile sending the request." error message is returned?

    Cause: The UriPattern parameter is configured in the common request parameters of the RPC-style API operation.

    Solution: Remove the UriPattern parameter from the common request parameters.