Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

C# Function Development

Updated on 2023-12-29 GMT+08:00

Function Syntax

NOTE:

You are advised to use .NET Core 3.1.

FunctionGraph supports C# (.NET Core 2.0), C# (.NET Core 2.1), and C# (.NET Core 3.1).

Scope Return parameter Function name (User-defined parameter, Context)

  • Scope: It must be defined as public for the function that FunctionGraph invokes to execute your code.
  • Return parameter: user-defined output, which is converted into a character string and returned as an HTTP response.
  • Function name: user-defined function name. The name must be consistent with that you define when creating a function.

    In the navigation pane on the left of the FunctionGraph console, choose Functions > Function List. Click the name of the function to be set. On the function details page that is displayed, choose Configuration > Basic Settings and set the Handler parameter, as shown in Figure 1. The parameter value is in the format of index.handler. The values of index and handler can be customized.

    Figure 1 Handler parameter
  • Event: event parameter defined for the function.
  • context: runtime information provided for executing the function. For details, see the description of SDK APIs.

    The HC.Serverless.Function.Common library needs to be referenced when you deploy a project in FunctionGraph. For details about the IFunctionContext object, see the context description.

    When creating a C# function, you need to define a method as the handler of the function. The method can access the function by using specified IFunctionContext parameters. Example:

    1
    2
    3
    4
    public Stream handlerName(Stream input,IFunctionContext context)
    {
           // TODO
    }
    

Function Handler

ASSEMBLY::NAMESPACE.CLASSNAME::METHODNAME

  • ASSEMBLY: name of the .NET assembly file for your application, for example, HelloCsharp.
  • NAMESPACE and CLASSNAME: names of the namespace and class to which the handler function belongs.
  • METHODNAME: name of the handler function. Example:

    Set the handler to HelloCsharp::Example.Hello::Handler when you create a function.

SDK APIs

  • Context APIs

    Table 1 describes the provided context attributes.

    Table 1 Context objects

    Attribute

    Description

    String RequestId

    Request ID.

    String ProjectId

    Project Id

    String PackageName

    Name of the group to which the function belongs.

    String FunctionName

    Function name.

    String FunctionVersion

    Function version.

    Int MemoryLimitInMb

    Allocated memory.

    Int CpuNumber

    CPU usage of a function.

    String Accesskey

    Obtains the AK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    NOTE:

    FunctionGraph has stopped maintaining the String AccessKey API in the Runtime SDK. You cannot use this API to obtain a temporary AK.

    String Secretkey

    Obtains the SK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    NOTE:

    FunctionGraph has stopped maintaining the String SecretKey API in the Runtime SDK. You cannot use this API to obtain a temporary SK.

    String SecurityAccessKey

    Obtains the SecurityAccessKey (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    String SecuritySecretKey

    Obtains the SecuritySecretKey (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    String SecuritySecretToken

    Obtains the SecuritySecretToken (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    String Token

    Obtains the token (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    Int RemainingTimeInMilliSeconds

    Remaining running time of a function.

    String GetUserData(string key,string defvalue=" ")

    Uses keys to obtain the values passed by environment variables.

  • Logging APIs

    The following table describes the logging APIs provided in the C# SDK.

    Table 2 Logging APIs

    Method

    Description

    Log(string message)

    Creates a logger object by using context.

    var logger = context.Logger;

    logger.Log("hello CSharp runtime test(v1.0.2)");

    Logf(string format, args ...interface{})

    Creates a logger object by using context.

    var logger = context.Logger;

    var version = "v1.0.2"

    logger.Logf("hello CSharp runtime test({0})", version);

Execution Result

The execution result consists of the function output, summary, and log output.

Table 3 Description of the execution result

Parameter

Successful Execution

Failed Execution

Function Output

The defined function output information is returned.

A JSON file that contains errorMessage and errorType is returned. The format is as follows:

{
    "errorMessage": "",
    "errorType": ""
}

errorMessage: Error message returned by the runtime.

errorType: Error type.

Summary

Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed.

Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed.

Log Output

Function logs are printed. A maximum of 4 KB logs can be displayed.

Error information is printed. A maximum of 4 KB logs can be displayed.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback