【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException
  ExOerB7z3frR 2023年11月30日 31 0

问题描述

创建PowerShell Azure Durable Function,执行大量的PowerShell脚本操作Azure Resource,遇见了一个非常非常奇怪的问题:

Function 'Hello1 (Activity)' failed with an error. Reason: Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded. Path '[9].Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.StorageAccount.BlobStorageUri', line 1, position 103037.

at Newtonsoft.Json.JsonReader.Push(JsonContainerType value)

at Newtonsoft.Json.JsonTextReader.ParseValue()

at Newtonsoft.Json.JsonWriter.WriteToken(JsonReader reader, Boolean writeChildren, Boolean writeDateConstructorAsDate, Boolean writeComments)

at Newtonsoft.Json.Linq.JTokenWriter.WriteToken(JsonReader reader, Boolean writeChildren, Boolean writeDateConstructorAsDate, Boolean writeComments)

Stack: .

【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException_Standard

 

问题解答

Function 'Hello1 (Activity)' failed with an error. Reason: Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded. 
Path '[9].Context.
Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.
Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.
Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.
Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.
Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.
Context.Context.Context.Context.Context.Context.Context.Context.Context.Context.
StorageAccount.BlobStorageUri', line 1, position 103037.

at Newtonsoft.Json.JsonReader.Push(JsonContainerType value)

 

因为异常中的Stack并没有指明是那一句代码引起的异常,所以只好使用最笨的办法。

一行代码一行代码的调试。

【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException_JSON_02

最终,在对代码进行逐句逐句的注释后执行,定位到是 New-AzStorageAccount的问题。当注释掉这一句后,问题消失。

New-AzStorageAccount -ResourceGroupName $rgName -Name $storageName -SkuName Standard_LRS -Location $region -AllowBlobPublicAccess $false  

 但是,为什么它会导致Function App执行出现如此诡异的问题呢?

 

问题原因

 

【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException_Standard_03

(单独执行 New-AzStorageAccount 代码,发现它的输出信息为一个表格对象)

 

Durable Function会把执行的输出进行转换为JSON Object并保存在Activity 函数的日志中。

因为Function Runtime在转换这个对象时,触发了Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded. 异常。 就是这个对象的深度达到了JSON的最大深度64。

解决办法

基于问题原因,可以主动修改New-AzStorageAccount 的结果输出,可以把输出到文件中,或者转换为 JSON格式,来避免问题。

方式一:把输出信息保存为文件,在命令后加上*> script.log

New-AzStorageAccount -ResourceGroupName $rgName -Name $storageName -SkuName Standard_LRS -Location $region -AllowBlobPublicAccess $false *> script.log

 

方式二:把输出对象转换为JSON, 在命令后加上| ConvertTo-Json

New-AzStorageAccount -ResourceGroupName $rgName -Name $storageName -SkuName Standard_LRS -Location $region -AllowBlobPublicAccess $false | ConvertTo-Json

 

经测试,两种方式都可以解决问题。

 

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月30日 0

暂无评论

推荐阅读
ExOerB7z3frR