.net6中Dapr之json时间格式化问题

1.原因

Dapr默认传输json是使用using System.Text.Json;进行格式化的

很多时候.net api接口使用的时间格式为yyyy/MM/dd HH:mm:ss:ffff

这样在使用dapr数据传输(服务调用)导致时间格式化错误

2.解决方案

在AddDapr()时加入序列化规则(Program.cs)

//Program.cs
  
  .AddDapr(option =>
    {
        var jsonOptions = new JsonSerializerOptions() {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            PropertyNameCaseInsensitive = true,
        };
        jsonOptions.Converters.Add(new DatetimeJsonConverter());//注入格式化时间
        option.UseJsonSerializationOptions(jsonOptions);
    })//dapr

3.心得

dapr没有做到直接跟随程序直接使用程序的序列化

导致产生很多问题,甚至怀疑程序问题导致服务调用错误

---最后---

博主开发的物联网管理平台正式完成1.0,欢迎申请使用。

http://wisdomgrid.cn/

你可能感兴趣的:(C#基础,Dapr,json)