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

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.

The following shows how to create a project named test using .NET Core 2.0 in Visual Studio 2017. The procedure is similar for .NET Core 2.1, C# (.NET Core 6.0, which is available only in LA-Mexico City2), and NET Core 3.1.

Creating a Project

  1. On the toolbar, choose File > New > Project, select .NET Core, select Class Library (.NET Core), and change the project name to test, as shown in Figure 1.
    Figure 1 Creating a project
  2. Select the test project in the navigation pane, right-click, and then choose Properties, as shown in Figure 2.
    Figure 2 Properties
  3. Choose Application and then set Target framework to .NET Core 2.0, as shown in Figure 3.
    Figure 3 Selecting a target framework

Adding a Reference

  1. Select the test project in Search Solution Explorer, right-click, and then choose Add Reference to reference the downloaded .dll file, as shown in Figure 4.
    Figure 4 Adding a reference

    Store HC.Serverless.Function.Common.dll, HC.Serverless.Function.Common.JsonSerializer.dll, and Newtonsoft.Json.dll in a lib file.

  2. Choose Browse, click Browse(B), reference the HC.Serverless.Function.Common.dll and HC.Serverless.Function.Common.JsonSerializer.dll files, and click OK, as shown in Figure 5.
    Figure 5 Referencing files
  3. View the references, as shown in Figure 6.
    Figure 6 References

Packing 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 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);
            }

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

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

        }
    }
}
  1. Right-click the test project and choose Build, as shown in Figure 7.
    Figure 7 Build
  2. Copy the path C:\Users\xxx\source\repos\test\test\bin\Release\netcoreapp2.0\ of the .dll files, as shown in Figure 8.
    Figure 8 Path of .dll files
    Figure 9 shows the files in the path.
    Figure 9 Files
  3. Create a test.runtimeconfig.json file in the path, as shown in Figure 10.
    Figure 10 New file
    Add the following content in the file:
    {
       
    "runtimeOptions": {
           
    "framework": {
               
    "name": "Microsoft.NETCore.App",
               
    "version": "2.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. If the framework is .NET Core 2.0, enter 2.0.0. If the framework is .NET Core 2.1, enter 2.1.0.
  4. Compress the file into netcoreapp2.0.zip.

    The package name can be customized but must be ended with .zip.

Test Example

  1. Create a C# (.NET 2.1) function on the FunctionGraph console and upload the code package, as shown in Figure 11.
    Figure 11 Uploading the code package
  2. Configure a test event, as shown in Figure 12. The key must be set to KetTest, and the value can be customized.
    Figure 12 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.