This topic describes the context object available when you write code in the C# runtime environment of Function Compute. When Function Compute runs your function, it passes a context object to the handler method. Use the context object to access invocation metadata, temporary credentials, and logging utilities — without hardcoding sensitive values in your function code.
Context properties
Each invocation receives a context object with the following properties:
RequestId(string) — The unique ID of the current invocation. Log this value to correlate requests when troubleshooting errors.Function(IFunctionParameter) — Basic information about the invoked function, including name, handler, memory, and timeout period.Credentials(ICredentials) — Temporary credentials that Function Compute gets by assuming your service-linked role. The credentials are valid for 36 hours and include an AccessKey ID, AccessKey secret, and security token. Use these credentials to access other Alibaba Cloud services such as Object Storage Service (OSS) without hardcoding an AccessKey pair in your function code. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.Logger(IFcLogger) — The logger encapsulated by Function Compute.Service(IServiceMeta) — Basic information about the service the function belongs to.OpenTracing
IFcContext interface
The IFcContext interface defines the full set of properties available in the context object. For the complete interface definition, see IFcContext.
public interface IFcContext
{
/// <summary>
/// The AliFc request ID associated with the request.
/// This is the same ID returned to the client that called invoke().
/// This ID is reused for retries on the same request.
/// </summary>
string RequestId { get; }
/// <summary>
/// Gets the function parameter interface.
/// </summary>
/// <value>The function parameter interface.</value>
IFunctionParameter FunctionParam {get;}
/// <summary>
/// AliFc logger associated with the Context object.
/// </summary>
IFcLogger Logger { get; }
/// <summary>
/// Gets the credentials interface.
/// </summary>
/// <value>The credentials interface.</value>
ICredentials Credentials {get;}
/// <summary>
/// Gets the account identifier.
/// </summary>
/// <value>The account identifier.</value>
string AccountId { get; }
/// <summary>
/// Gets the region.
/// </summary>
/// <value>The region.</value>
string Region { get; }
/// <summary>
/// Gets the service meta.
/// </summary>
/// <value>The service meta.</value>
IServiceMeta ServiceMeta { get; }
}Use temporary credentials to access OSS
The following example reads temporary credentials from the context object and uses them to upload an object to OSS. The function requires the service-linked role to have the AliyunOSSFullAccess policy attached. The recommended role is AliyunFCDefaultRole.
Using the AccessKey pair of an Alibaba Cloud account to perform operations in Function Compute is a high-risk practice. Use a RAM user or the service-linked role instead. Never store an AccessKey pair directly in your function code.
For a reference implementation, see dotnet3-oss.
using System;
using System.IO;
using Aliyun.OSS;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Logging;
namespace Example
{
public class OssExample
{
public Stream HandleRequest(Stream stream, IFcContext context)
{
context.Logger.LogInformation("Handle request: {0}", context.RequestId);
// The bucket name. Create the bucket before running this function.
string bucketName = "my-****";
// The full path of the object to upload.
string objectName = "exampledir/exampleobject.txt";
// Use the internal endpoint of the region where the bucket resides.
// This example uses the internal endpoint for the China (Hangzhou) region.
string endpoint = "https://oss-cn-hangzhou-internal.aliyuncs.com";
// Get temporary credentials from the context object.
// These credentials are issued by the service-linked role configured for the function.
ICredentials creds = context.Credentials;
// Create an OssClient instance using the temporary credentials.
OssClient ossClient =
new OssClient(endpoint,
creds.AccessKeyId,
creds.AccessKeySecret,
creds.SecurityToken);
// Upload the input stream as an object.
ossClient.PutObject(bucketName, objectName, stream);
// Retrieve the uploaded object and return its content.
OssObject obj = ossClient.GetObject(bucketName, objectName);
context.Logger.LogInformation("Put object to OSS success: {0}", obj.ToString());
return obj.Content;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}