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
- 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.
- Select the test project in the navigation pane, right-click, and then choose Properties, as shown in Figure 2.
- Choose Application and then set Target framework to .NET Core 2.0, as shown in Figure 3.
Adding a Reference
- 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.
Store HC.Serverless.Function.Common.dll, HC.Serverless.Function.Common.JsonSerializer.dll, and Newtonsoft.Json.dll in a lib file.
- 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.
- View the references, as shown in Figure 6.
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. } } } |
- Right-click the test project and choose Build, as shown in Figure 7.
- Copy the path C:\Users\xxx\source\repos\test\test\bin\Release\netcoreapp2.0\ of the .dll files, as shown in Figure 8.
Figure 9 shows the files in the path.
- Create a test.runtimeconfig.json file in the path, as shown in Figure 10.
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.
- Compress the file into netcoreapp2.0.zip.
The package name can be customized but must be ended with .zip.
Test Example
- Create a C# (.NET 2.1) function on the FunctionGraph console and upload the code package, as shown in Figure 11.
- Configure a test event, as shown in Figure 12. The key must be set to KetTest, and the value can be customized.
- The attribute of the serialization class must be defined as KetTest.
- The test string must be in JSON format.
- Click Test and view the execution result.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot