.NET SDK examples
This topic uses the .NET SDK V2.0 as an example to demonstrate how to call Cloud Monitor 2.0 (cms20240330) OpenAPI operations. The calling method is similar for other operations. You only need to replace the request and response types and fields.
Prerequisite
.NET Framework 4.5 or later, or .NET Core 2.0 or later.
Installation
Install via the dotnet CLI:
dotnet add package AlibabaCloud.SDK.Cms20240330 --version 10.0.0Alternatively, add the following to your project .csproj file:
<PackageReference Include="AlibabaCloud.SDK.Cms20240330" Version="10.0.0" />Source code repository: alibabacloud-csharp-sdk/cms-20240330.
Configure access credentials
We recommend that you use a more secure method that does not require an AccessKey pair for your project code. For more information about credential configuration, see Manage access credentials. The simplest approach is to configure environment variables:
export ALIBABA_CLOUD_ACCESS_KEY_ID="<your-access-key-id>"
export ALIBABA_CLOUD_ACCESS_KEY_SECRET="<your-access-key-secret>"Aliyun.Credentials.Client searches for credentials in the order defined by the default credential chain.
Complete code example
The following example calls the GetCmsService operation to query the activation status of a Cloud Monitor 2.0 service for a specific product:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/// <summary>
/// Initialize a client using credentials.
/// </summary>
public static AlibabaCloud.SDK.Cms20240330.Client CreateClient()
{
// For production code, we recommend that you use a more secure method that does not require an AccessKey pair.
Aliyun.Credentials.Client credential = new Aliyun.Credentials.Client();
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
Credential = credential,
};
// For more information about the endpoint, see https://api.aliyun.com/product/Cms.
config.Endpoint = "metrics.cn-hangzhou.aliyuncs.com";
return new AlibabaCloud.SDK.Cms20240330.Client(config);
}
public static void Main(string[] args)
{
AlibabaCloud.SDK.Cms20240330.Client client = CreateClient();
AlibabaCloud.SDK.Cms20240330.Models.GetCmsServiceRequest request = new AlibabaCloud.SDK.Cms20240330.Models.GetCmsServiceRequest
{
Product = "your_value",
Service = "your_value",
};
Dictionary<string, string> headers = new Dictionary<string, string>() { };
try
{
AlibabaCloud.SDK.Cms20240330.Models.GetCmsServiceResponse resp = client.GetCmsServiceWithOptions(request, headers, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
Console.WriteLine(JsonConvert.SerializeObject(resp, Formatting.Indented));
}
catch (TeaException error)
{
// The error message.
Console.WriteLine(error.Message);
// The recommended URL for troubleshooting.
Console.WriteLine(error.Data["Recommend"]);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
Console.WriteLine(error.Message);
Console.WriteLine(error.Data["Recommend"]);
}
}
}
}Procedure
The code works as follows:
Initialize the Config object
AlibabaCloud.OpenApiClient.Models.Config. The Config object stores the Credential, Endpoint, and other configurations. The Endpoint ismetrics.cn-hangzhou.aliyuncs.comas shown in the example.Aliyun.Credentials.Client credential = new Aliyun.Credentials.Client(); AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config { Credential = credential, }; config.Endpoint = "metrics.cn-hangzhou.aliyuncs.com";Instantiate the client:
AlibabaCloud.SDK.Cms20240330.Client client = new AlibabaCloud.SDK.Cms20240330.Client(config);Create the Request object for the API operation:
GetCmsServiceRequest request = new GetCmsServiceRequest();Set request parameters:
request.Product = "your_value"; request.Service = "your_value";Call the API operation through the client:
GetCmsServiceResponse response = client.GetCmsService(request); Console.WriteLine(JsonConvert.SerializeObject(response.Body));Read return values from the response, for example:
string requestId = response.Body.RequestId;Use a catch block to handle errors:
catch (Exception e) { Console.WriteLine(e.StackTrace); }
For more API operations and sample code, see SDK examples on the OpenAPI portal.