更新时间:2025-07-18 GMT+08:00
分享

.NET SDK使用范例

同步执行函数为例,提供封装后的SDK。为了保证您成功使用以下示例代码,您需要先添加对应语言的SDK依赖。

表1 .NET SDK信息说明

SDK信息

说明

安装

  • 使用.NET CLI工具
    dotnet add package HuaweiCloud.SDK.FunctionGraph
  • 使用Package Manager
    Install-Package HuaweiCloud.SDK. FunctionGraph

相关链接

SDK依赖包地址

.NET SDK使用说明

SDK的请求参数、响应参数、请求示例和响应示例与对应的API一致,具体参数和示例说明请参见同步执行函数接口。

SDK调用示例

using System;
using System.Collections.Generic;
using HuaweiCloud.SDK.Core;
using HuaweiCloud.SDK.Core.Auth;
using HuaweiCloud.SDK.FunctionGraph;
using HuaweiCloud.SDK.FunctionGraph.V2;
using HuaweiCloud.SDK.FunctionGraph.V2.Model;
namespace InvokeFunctionSolution
{
    class Program
    {
        static void Main(string[] args)
        {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            string ak = Environment.GetEnvironmentVariable("CLOUD_SDK_AK");
            string sk = Environment.GetEnvironmentVariable("CLOUD_SDK_SK");
            var config = HttpConfig.GetDefaultConfig();
            config.IgnoreSslVerification = true;
            var auth = new BasicCredentials(ak, sk);
            var client = FunctionGraphClient.NewBuilder()
                    .WithCredential(auth)
                    .WithRegion(FunctionGraphRegion.ValueOf("<regionId>"))
                    .WithHttpConfig(config)
                    .Build();
            var req = new InvokeFunctionRequest
            {
            };
            try
            {
                var resp = client.InvokeFunction(req);
                var respStatusCode = resp.HttpStatusCode;
                Console.WriteLine(respStatusCode);
            }
            catch (RequestTimeoutException requestTimeoutException)
            {
                Console.WriteLine(requestTimeoutException.ErrorMessage);
            }
            catch (ServiceResponseException clientRequestException)
            {
                Console.WriteLine(clientRequestException.HttpStatusCode);
                Console.WriteLine(clientRequestException.RequestId);
                Console.WriteLine(clientRequestException.ErrorCode);
                Console.WriteLine(clientRequestException.ErrorMsg);
            }
            catch (ConnectionException connectionException)
            {
                Console.WriteLine(connectionException.ErrorMessage);
            }
        }
    }
}

AK/SK、region(EndPoint)、project_id(项目ID)的获取请参见AK/SK签名认证操作指导

func_name、version在函数界面上获取。

xCFFRequestVersionRequest:返回体格式,取值v0,v1。v0:默认返回文本格式;v1:默认返回json格式,sdk需要使用此值。

相关文档