【问题标题】:Azure Durable Functions & Json dynamic arrayAzure Durable Functions 和 Json 动态数组
【发布时间】:2022-10-18 21:27:46
【问题描述】:

我正在使用 Azure 持久函数,但在使用 System.Text.Json 时遇到了一些问题 在真实场景中,我将有一个活动函数,它将调用一个 API 端点并获得一些 Json 结果,该结果将有一个具有多种类型(字符串和 int)的数组。

我将尝试描述问题。你知道修复它的方法吗?

所以我使用这个类将它反序列化为一个 C# 对象。

public class JsonTest
{
    public dynamic[] Test { get; set; }
}

这是执行反序列化的示例活动函数。

[FunctionName("GetJson")]
    public static JsonTest GetJson([ActivityTrigger] string id)
    {
        var jsonString = "{ \"Test\": [\"Fabrizio\", 39] }";

        var result = JsonSerializer.Deserialize<JsonTest>(jsonString);

        return result;
    }

我可以看到它有效。

Imgur

但是,一旦我将对象从 Activity Function 返回到 Orchestrator,就会出现问题。我已经没有价值观了。即使我尝试扩展它。我最终会得到一个错误。 它也使用 Newtonsoft.Json.Linq.JToken 但我不知道它有多相关,因为我只是使用 System.text.json

第一='(新 System.Linq.SystemCore_EnumerableDebugView<System.Collections.Generic.KeyValuePair<string, Newtonsoft.Json.Linq.JToken>>(foo.Test[0]).Items[0]).Value.First' 引发了“System.InvalidOperationException”类型的异常

Imgur Imgur

【问题讨论】:

    标签: c# json azure-functions system.text.json azure-durable-functions


    【解决方案1】:

    我已经复制了代码并得到了预期的结果。

    试试下面的代码:JsonTest.cs

      public class JsonTest
      {     
         public dynamic[] Test { get; set; }
      }
    

    活动功能:

      [FunctionName("GetJson")]
         public static JsonTest GetJson([ActivityTrigger] string id)
         {
         var json = "{ "Test": ["Fabrizio", 39] }"; 
             var result = JsonConvert.DeserializeObject<JsonTest>(json);
             Console.WriteLine($"Deserialized JSON into {result.GetType()}");
             return result;
    
         }
    

    编排器功能:

    [FunctionName("Function")]
         public static async Task<object> RunOrchestrator(
             [OrchestrationTrigger] IDurableOrchestrationContext context)
         {
           return await context.CallActivityAsync<object>("GetJson", "London");
             
         }
    

    输出:

    【讨论】:

      猜你喜欢
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 2022-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2022-06-10
      相关资源
      最近更新 更多