Updated on 2024-11-11 GMT+08:00

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 using .NET Core 2.1. The procedure is similar for C# (.NET Core 6.0, which is only available in LA-Mexico City2) and NET Core 3.1. The .NET SDK 2.1 has been installed in the execution environment.

Creating a Project

  1. Run the following command to create the /tmp/csharp/projets /tmp/csharp/release directory:
    mkdir -p  /tmp/csharp/projets;mkdir -p  /tmp/csharp/release
  2. Run the following command to go to the /tmp/csharp/projets/ directory:
    cd /tmp/csharp/projets/
  3. Create the project file test.csproj with the following content:
    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</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. Upload the .dll file.

    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/projets/ directory.

  2. Create the Class1.cs file in the /tmp/csharp/projets/ 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:
    /home/tools/dotnetcore-sdk/dotnet-sdk-2.1.302-linux-x64/dotnet build /tmp/csharp/projets/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": "2.1.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. If the framework is .NET Core 2.0, enter 2.0.0. If the framework is .NET Core 2.1, enter 2.1.0.
    • For .NET Core 2.0, check whether Newtonsoft.Json is referenced in the *.deps.json file. If Newtonsoft.Json is not referenced, reference it manually.
      1. Reference the following content in targets:
        "Newtonsoft.Json/9.0.0.0": {
                "runtime": {
                  "Newtonsoft.Json.dll": {
                    "assemblyVersion": "9.0.0.0",
                    "fileVersion": "9.0.1.19813"
                  }
                }
              }
      2. Reference the following content in libraries:
        "Newtonsoft.Json/9.0.0.0": {
              "type": "reference",
              "serviceable": false,
              "sha512": ""
            }
  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 2.1) function 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.
    Figure 2 Configuring a test event

    • The attribute of the serialization class must be defined as KetTest.
    • The test string must be in JSON format.
  3. Click Test and view the execution result.