Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
    • Overview
      • Overview
      • Function interface
      • YcFunction interface
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  • FAQ

In this article:

  • Example
  • Function information output
  1. Developing in .NET Core
  2. Programming model
  3. YcFunction interface

Using the YcFunction interface to set a handler function in C#

Written by
Yandex Cloud
Updated at September 23, 2024
  • Example
    • Function information output

You can set a handler function in C# by implementing the YcFunction interface. To do this, add an SDK to the dependencies.

Warning

You should specify both values for the YcFunction type parameters: the first one being the input argument type and the second one, the type of the returned value. The handle method also has invocation context as its second argument.

Example of a valid handler:

public class Handler : YcFunction<int, String> {
  public String FunctionHandler(int i, Context c) {
    return String.valueOf(i);
  }
}

Examples of invalid handlers:

// YcFunction has only one parameter type specified
// Handler should not have any type parameters
public class Handler<T> : YcFunction<T, int> {
  public int FunctionHandler(T i, Context c) {
    return 2;
  }
}
// YcFunction has neither of the parameter types specified
public class Handler : YcFunction {
  public Object FunctionHandler(Object i, Context c) {
    return i;
  }
}

To learn more about the handler requirements, see C# programming model.

You can use any classes as input and return types.

Note

Fields of these classes may have any access modifiers. If a field is non-public, it requires the getter public method. Otherwise, the field will not be included in the response.

ExampleExample

Function information outputFunction information output

The following function:

  1. Receives a number as an input.
  2. Outputs the function data obtained from the invocation context to the execution log.
  3. Returns data on whether the received number is even.

Warning

To invoke the function, use the Yandex Cloud CLI or an HTTP request with the ?integration=raw parameter.

The Handler.cs file:

using Yandex.Cloud.Functions;

public class Handler : YcFunction<int, bool> {

  public bool FunctionHandler(int number, Context c) {
    Console.WriteLine($"Function name: {c.FunctionId}");
    Console.WriteLine($"Function version: {c.FunctionVersion}");
    Console.WriteLine($"Service account token: {c.TokenJson}");
    return number % 2 == 0;
  }
}

Example of input data:

41

The log will contain the following:

Function name: <function_name>
Function version: <function_version_ID>
Service account token: <service_account_token>

Returned string:

false

Was the article helpful?

Previous
Function interface
Next
Managing dependencies
Yandex project
© 2025 Yandex.Cloud LLC