在 Azure 队列触发函数中作为 ITableEntity 读取 Azure 存储表时出现 System.Text.Json 异常

问题描述 投票:0回答:1

我正在按照 Microsoft 的此示例从 azure 存储帐户的表中读取表实体,但收到以下错误消息:

错误:System.Text.Json.JsonException:JSON 值无法转换为 MyModels.MyTableData。路径: $ |行号: 0 |字节位置内联:1.

当我将表的内容读取为

object

 时,我看到表实体的数据采用 Json 格式,但无法反序列化为实现 
ITableEntity
 的目标对象。我在这次练习中缺少什么?

azure-functions azure-table-storage azure-storage-account
1个回答
0
投票
要从 Azure 存储获取表并使用

ITableEntity

,您可以使用下面对我有用的
代码

函数1.cs:

using System; using System.Collections.Generic; using System.Text.Json; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; using Azure.Data.Tables; using Microsoft.Extensions.Configuration; using Azure; namespace FunctionApp100 { public class RithTable : ITableEntity { public string Name { get; set; } public string PartitionKey { get; set; } public string RowKey { get; set; } public DateTimeOffset? Timestamp { get; set; } public string Id { get; set; } public ETag ETag { get; set; } } public static class Function1 { [FunctionName("Function1")] public static void Run( [QueueTrigger("myqueue-items", Connection = "rithconec")] string rith, ILogger log, ExecutionContext context) { log.LogInformation($"Hello Rithwik Bojja, The Table name given in Queue is : {rith}"); var rithconf = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var rithcon = rithconf["Values:rithconec"]; var RithTable = rith; var rith_tc = new TableClient(rithcon, RithTable); var rith_ent = new List<RithTable>(); foreach (var ri in rith_tc.Query<RithTable>()) { rith_ent.Add(ri); } foreach (var ri in rith_ent) { var rith_ser = JsonSerializer.Serialize(ri); log.LogInformation($"Hello Rithwik Bojja, The Serialized Value is : {rith_ser}"); } } } }

csproj:

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> <UserSecretsId>7d9865g n-7gs80-9931</UserSecretsId> <NoWarn>NU1605</NoWarn> </PropertyGroup> <ItemGroup> <PackageReference Include="Azure.Data.Tables" Version="12.8.3" /> <PackageReference Include="Azure.Storage.Blobs" Version="12.16.0" /> <PackageReference Include="Azure.Storage.Files.Shares" Version="12.1.0" /> <PackageReference Include="Azure.Storage.Queues" Version="12.14.0" /> <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.3.0" /> <PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.3" /> <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" /> <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Tables" Version="1.2.1" /> </ItemGroup> <ItemGroup> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> </ItemGroup> </Project>

local.settings.json:

{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet", "rithconec": "constrng" } }

存储帐户中的表:

enter image description here

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.