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

Developing a C# Function Using .NET Core CLI

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.

The following shows how to create a project named test based on .NET Core 6.0. The procedure is similar for other versions. The .NET SDK has been installed in the execution environment.

Creating a Project

  1. Run the following command to create the /tmp/csharp/projects /tmp/csharp/release directory:
    mkdir -p  /tmp/csharp/projects;mkdir -p  /tmp/csharp/release
  2. Run the following command to go to the /tmp/csharp/projects/ directory:
    cd /tmp/csharp/projects/
  3. Create the project file test.csproj with the following content:
    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <RootNamespace>test</RootNamespace>
        <AssemblyName>test</AssemblyName>
      </PropertyGroup>
      <ItemGroup>
        <Reference Include="HC.Serverless.Function.Common">
          <HintPath>HC.Serverless.Function.Common.dll</HintPath>
        </Reference>
        <Reference Include="HC.Serverless.Function.Common.JsonSerializer">
          <HintPath> HC.Serverless.Function.Common.JsonSerializer.dll</HintPath>
        </Reference>
      </ItemGroup>
    </Project>

Generating a Code Library

  1. Download the dll files.

    Upload the HC.Serverless.Function.Common.dll, HC.Serverless.Function.Common.JsonSerializer.dll, and Newtonsoft.Json.dll files in the package to the /tmp/csharp/projects/ directory.

  2. Create the Class1.cs file in the /tmp/csharp/projects/ directory. The code is as follows:
     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
    30
    31
    32
    33
    34
    using HC.Serverless.Function.Common;
    using System;
    using System.IO;
    
    namespace test
    {
        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);
    
                }
                else
                {
                    return null;
                }
    
              return test.Serialize<TestJson>(Testjson);
            }
    
            public class TestJson
            {
                public string KetTest { get; set; }//Define the attribute of the serialization class as KetTest.
    
            }
        }
    }
    
  3. Run the following command to generate a code library:
    dotnet build /tmp/csharp/projects/test.csproj -c Release -o /tmp/csharp/release

    .NET directory: /home/tools/dotnetcore-sdk/dotnet-sdk-2.1.302-linux-x64/dotnet

  4. Run the following command to go to the /tmp/csharp/release directory:
    cd /tmp/csharp/release 
  5. View the compiled .dll files in the /tmp/csharp/release directory.
    -rw-r--r-- 1 root root 468480 Jan 21 16:40 Newtonsoft.Json.dll
    -rw-r--r-- 1 root root   5120 Jan 21 16:40 HC.Serverless.Function.Common.JsonSerializer.dll
    -rw-r--r-- 1 root root   5120 Jan 21 16:40 HC.Serverless.Function.Common.dll
    -rw-r--r-- 1 root root    232 Jan 21 17:10 test.pdb
    -rw-r--r-- 1 root root   3584 Jan 21 17:10 test.dll
    -rw-r--r-- 1 root root   1659 Jan 21 17:10 test.deps.json
  6. Create the test.runtimeconfig.json file in the /tmp/csharp/release directory. The file content is as follows:
    {
        "runtimeOptions": {
            "framework": {
                "name": "Microsoft.NETCore.App",
                "version": "6.0.0"
            }
        }
    }
  7. Run the following command to package the test.zip file in the /tmp/csharp/release directory:
    zip -r test.zip ./*

Test Example

  1. Create a C# (.NET 6.0) event function from scratch on the FunctionGraph console and upload the test.zip file, as shown in Figure 1.
    Figure 1 Uploading the code package
  2. Configure a test event, as shown in Figure 2. The key must be set to KetTest, and the value can be customized. (The test string must be in JSON format.)
    Figure 2 Configuring a test event

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

  3. Click Test and view the execution result.