Building FunctionGraph Event Functions with C# from scratch¶
Following chapter describes how to build FunctionGraph event functions using C# from scratch:
Introduction¶
For general details about creating event functions from scratch and executing an event function, see Creating a Function from Scratch and Executing the Function in the user manual.
Function Development Overview¶
The C# function handler is the method in your function code that processes events.
When your function is invoked, FunctionGraph runs the handler method. Your function runs until the handler returns a response, exits, or times out.
C# function syntax: [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. For C# functions, the return parameter type must be defined as Stream.
Function name: user-defined function name. The name must be consistent with that you define when creating a function.
User-defined parameter: parameter defined for the function. The type must be defined as Stream.
context: runtime information provided for executing the function.
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.
Example:
#if NET6_0_OR_GREATER
using OpenTelekomCloud.Serverless.Function.Common;
#else
using HC.Serverless.Function.Common;
#endif
namespace src
{
public class Program
{
/// Handler name: src.Program::Handler
public Stream Handler(Stream inputEvent, IFunctionContext context)
{
return "hello world";
}
}
/// <summary>
/// Main method - not used in FunctionGraph but needed for compilation
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
Console.WriteLine("This is a FunctionGraph C# runtime program");
}
}
Note
As the project output type has to be set in .csproj file to
<OutputType>Exe</OutputType>, a Main method is required.
This method is needed for compilation only and is not invoked by FunctionGraph.
Note
For C# versions
prior to .NET 6.0, classes from
HC.Serverless.Function.Commonmust be used.NET 6.0 and above, classes from
OpenTelekomCloud.Serverless.Function.Commonmust be used.
Handler¶
For a C# function, the handler must be named in the format of
ASSEMBLY::NAMESPACE.CLASSNAME::METHODNAME
ASSEMBLY: name of the .NET assembly file for your application, for example: simple.
NAMESPACE and CLASSNAME: names of the namespace and class to which the handler function belongs (as defined in Program.cs: src.Program).
METHODNAME: name of the handler function (as defined in Program.cs: Handler).
simple::src.Program::Handler
Initializer¶
For details about the initializer, see Initializer function.
The initializer is in the format of ASSEMBLY::NAMESPACE.CLASSNAME::METHODNAME.
The following is a simple initializer:
public Stream Initializer(IFunctionContext context)
{
var logger = context.Logger;
logger.Logf("Hello World");
return "hello world";
}
Function name: The function name Initializer must be the initializer function name specified for a function.
For example, if the initializer is named simple::src.Program::Initializer, FunctionGraph loads the initializer function defined in the simple::src.Program file.
context: The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata. See Using the FunctionGraph context interface to retrieve C# function information for details.
Accessing and using the FunctionGraph context object¶
The Context interface allows functions to obtain the function execution context, such as information about the invocation, function, execution environment, and so on.
The context is of type IFunctionContext
and is the second argument of the handler function.
Logging in FunctionGraph¶
To produce logs in T Cloud Public Log Tank Service (LTS) you can use
context.Logger to get a RuntimeLogger object for logging.
var logger = context.Logger;
var payload ="World";
logger.Logf("Hello {0}", string(payload));
For more information about the context object, see Using the FunctionGraph context object to retrieve function information.
Accessing environment¶
Environment variables defined in T Cloud Public FunctionGraph Console >
Configuration > Environment Variables can be accessed using:
// accessing an environment variable named "ENV_VAR1"
context.GetUserData("ENV_VAR1","");