All Products
Search
Document Center

ApsaraVideo Live:Use the server SDK for .NET

Last Updated:Dec 22, 2023

This topic describes how to use the server SDK for .NET provided by ApsaraVideo Live and provides relevant sample code.

Prerequisites

  • .NET Framework 4.5 or later is installed.

  • The server SDK for .NET is downloaded. For more information, see SDK download.

Procedure

  1. Install the SDK.

    1. On the download page of Alibaba Cloud SDKs, copy the DLL Reference for .NET 4.0 and Later link for ApsaraVideo Live and open the link in a new window to download the DLL file. For more information, see SDK download.

    2. In Solution Explorer of Visual Studio, right-click your project and choose Add > Reference.

    3. In the Reference Manager dialog box, click Browse, select the downloaded DLL file, and then click Add.

    4. Click OK.

  2. Create a configuration file named config.ini and place it in the conf directory. Include your AccessKey ID and AccessKey secret in the configuration file. Example:

    [default]
    access_key_id = YOUR_ACCESS_KEY_ID
    access_key_secret = YOUR_ACCESS_KEY_SECRET

    Replace YOUR_ACCESS_KEY_ID and YOUR_ACCESS_KEY_SECRET with your actual AccessKey ID and AccessKey secret.

    Then, you can use the following C# code to read the configuration file and invoke the SDK.

  3. Create a DefaultAcsClient instance and initialize the instance.

    var config = new IniConfig();
    config.Load(File.OpenRead("conf/config.ini"));
    
    // The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
    // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
    // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. 
    var accessKeyId = config.Get("default", "access_key_id");
    var accessKeySecret = config.Get("default", "access_key_secret");
    
    // Create an AcsClient instance.
    var profile = DefaultProfile.GetProfile(<your-region-id>, accessKeyId, accessKeySecret);
    var client = new DefaultAcsClient(profile);
  4. Initiate the API request and handle the response or exception.

    using System;
    using System.IO;
    using Aliyun.Acs.Core;
    using Aliyun.Acs.Core.Exceptions;
    using Aliyun.Acs.Core.Profile;
    using Aliyun.Acs.Core.Retry;
    using Aliyun.Acs.Core.Retry.Condition;
    using Aliyun.Acs.Core.Transform;
    using Aliyun.Acs.Core.Utils;
    using Aliyun.Acs.live.Model.V20161101;
    
    class TestProgram
    {
        static void Main(string[] args)
        {
            // Create a client to send a request.
            var config = new IniConfig();
            config.Load(File.OpenRead("conf/config.ini"));
    
            // The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
            // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
            // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. 
            var accessKeyId = config.Get("default", "access_key_id");
            var accessKeySecret = config.Get("default", "access_key_secret");
    
            // Create an AcsClient instance.
            var profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            var client = new DefaultAcsClient(profile);
            try
            {
                // Construct a request.
                DescribeInstancesRequest request = new DescribeInstancesRequest();
                request.PageSize = 10;
                // Send the request and obtain the response.
                DescribeInstancesResponse response = client.GetAcsResponse(request);
                System.Console.WriteLine(response.TotalCount);
            }
            catch (ServerException ex)
            {
                System.Console.WriteLine(ex.ToString());
            }
            catch (ClientException ex)
            {
                System.Console.WriteLine(ex.ToString());
            }
        }
    }