更新时间:2024-03-07 GMT+08:00
分享

使用NET Core CLI

C#新增json序列化和反序列化接口,并提供HC.Serverless.Function.Common.JsonSerializer.dll 。

提供的接口如下:

T Deserialize<T>(Stream ins):反序列化至传递到Function处理程序的对象中。

Stream Serialize<T>(T value):序列化至传递到返回的响应负载中。

本例以.NET Core2.1创建“test”工程为例说明,.NET Core2.0、.NET Core3.1、C#(.NET Core 6.0,当前仅支持华北-乌兰察布二零二)方法类似。执行环境已装有.NET SDK2.1。

新建项目

  1. 创建目录“/tmp/csharp/projets /tmp/csharp/release” ,执行命令如下:
    mkdir -p  /tmp/csharp/projets;mkdir -p  /tmp/csharp/release
  2. 进入“/tmp/csharp/projets/”目录,执行命令如下:
    cd /tmp/csharp/projets/
  3. 新建工程文件 “test.csproj”,文件内容如下:
    <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>

生成代码库

  1. 上传dll文件

    将HC.Serverless.Function.Common.dll、HC.Serverless.Function.Common.JsonSerializer.dll、Newtonsoft.Json.dll文件上传至目录“/tmp/csharp/projets/”。

  2. 在“/tmp/csharp/projets/”路径下,新建“Class1.cs”文件,代码内容如下:
     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; }//定义序列化的类中的属性为KetTest
    
            }
        }
    }
    
  3. 执行以下命令,生成代码库:
    /home/tools/dotnetcore-sdk/dotnet-sdk-2.1.302-linux-x64/dotnet build /tmp/csharp/projets/test.csproj -c Release -o /tmp/csharp/release

    dotnet的路径:/home/tools/dotnetcore-sdk/dotnet-sdk-2.1.302-linux-x64/dotnet。

  4. 执行以下命令,进入“/tmp/csharp/release”路径。
    cd /tmp/csharp/release 
  5. 在路径“/tmp/csharp/release”下查看编译生成的dll文件,如下所示:
    -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. 在“/tmp/csharp/release”路径下,新建文件“test.runtimeconfig.json”文件,文件内容如下:
    {
        "runtimeOptions": {
            "framework": {
                "name": "Microsoft.NETCore.App",
                "version": "2.1.0"
            }
        }
    }
    • *.runtimeconfig.json文件的名称为程序集的名称。
    • 文件内容中的version:项目属性中的目标框架的版本号,2.0则为2.0.0 ,2.1则为2.1.0。
    • 当目标框架为.NET Core2.0时,要注意生成*.deps.json文件中是否已引入Newtonsoft.Json。如果没有引入,则需要自己手动引入,如下所示:
      1. 需要在“targets”中引入以下内容:
        "Newtonsoft.Json/9.0.0.0": {
                "runtime": {
                  "Newtonsoft.Json.dll": {
                    "assemblyVersion": "9.0.0.0",
                    "fileVersion": "9.0.1.19813"
                  }
                }
              }
      2. 在“libraries”引入以下内容:
        "Newtonsoft.Json/9.0.0.0": {
              "type": "reference",
              "serviceable": false,
              "sha512": ""
            }
  7. 在“/tmp/csharp/release”路径下,执行如下命令,打包test.zip代码库压缩包。
    zip -r test.zip ./*

测试示例

  1. 在华为云FunctionGraph控制台新建一个C#(.NET 2.1)函数,上传打包好的“test.zip”压缩包,如图1所示。
    图1 上传代码包
  2. 配置一个测试事件。如图2所示。其中的key必须设置为“KetTest”,value可以自定义。
    图2 配置测试事件

    • KetTest:定义序列化的类中的属性为KetTest。
    • 测试串必需为json格式。
  3. 单击“测试”,查看测试执行结果。
分享:

    相关文档

    相关产品