Updated on 2025-12-10 GMT+08:00

Developing a C# Function Using Visual Studio

C# supports JSON serialization and deserialization interfaces and provides the HC.Serverless.Function.Common.JsonSerializer.dll file.

The interfaces are as follows:

T Deserialize<T>(Stream ins): Deserializes data into objects of function programs.

Stream Serialize<T>(T value): Serializes data to the returned response payload.

This section uses Visual Studio 2022 as an example to describe how to create a .NET Core 6.0 ClassLibrary project. The procedure for other runtime versions is similar.

Step 1: Creating a Project

  1. In Visual Studio, select Create a new project, select Class Library as shown in Figure 1, click Next, and select .NET 6.0 to create a project.
    Figure 1 Creating a project

  2. As shown in Figure 2, right-click the ClassLibrary project in the Solution Explorer and choose Add Project Reference from the shortcut menu.
    Figure 2 Adding a reference

  3. Choose Browse, click Browse (B), select the three libraries in the downloaded .dll file for reference, and click OK.

    The .dll file contains three libraries: HC.Serverless.Function.Common.dll, HC.Serverless.Function.Common.JsonSerializer.dll and Newtonsoft.Json.dll.

  4. View the references, as shown in Figure 3.
    Figure 3 References

Step 2: Packaging Code

Sample code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using HC.Serverless.Function.Common;
using System;
using System.IO;

namespace ClassLibrary2
{
    public class Class1
    {
        public Stream ContextHandlerSerializer(Stream input, IFunctionContext context)
        {
            var logger = context.Logger;
            logger.Logf("CSharp runtime test(v1.0.2)");
            JsonSerializer test = new JsonSerializer();
            TestJson Testjson = test.Deserialize<TestJson>(input);
            if (Testjson != null)
            {
                logger.Logf("json Deserialize KetTest={0}", Testjson.KetTest);
            }

            return test.Serialize<TestJson>(Testjson);
        }

        public class TestJson
        {
            public string KetTest { get; set; }//Define the attribute of the serialization class as KetTest.

        }
    }
}
  1. As shown in Figure 4, choose Build > Build Solution from the menu bar and copy the path of the generated .dll file in the output box.
    Figure 4 Generating files

  2. View the generated file in the local folder, as shown in Figure 5.
    Figure 5 Files
  3. Create the ClassLibrary.runtimeconfig.json file in the folder and add the following content to the file: After the operation is complete, there are seven files in total.
    {
        "runtimeOptions": {
            "framework": {
                "name": "Microsoft.NETCore.App",
                "version": "6.0.0"
            }
        }
    }
    • The name of the *.runtimeconfig.json file is the name of an assembly.
    • The Version parameter in the file indicates the version number of the target framework.
  4. Compress the files in the folder into a .zip package. Do not compress the entire folder. Ensure that seven files are displayed after the package is decompressed.

Step 3: Testing the Function

  1. On the FunctionGraph console, create a C# (.NET Core 6.0) event function from scratch and upload the ZIP code package created in 4, as shown in Figure 6.
    Figure 6 Uploading the code package

  2. Choose Configuration > Basic Settings, set Handler to ClassLibrary2::ClassLibrary2.Class1::ContextHandlerSerializer, and click Save, as shown in Figure 7.
    Figure 7 Configuring the function handler

  3. Return to the Code tab page, click Test to configure a test event. As shown in Figure 8, change key to KetTest to identify the corresponding value.
    Figure 8 Configuring a test event

    The attribute of the serialization class must be defined as KetTest.

  4. Click Test and view the execution result.
    Figure 9 Viewing the execution result