Reputation: 4260
I am following this example by Microsoft to read a table entity from an azure storage account's table but I am getting the following error message:
Error:System.Text.Json.JsonException: The JSON value could not be converted to MyModels.MyTableData. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
When I read the table's content as an object
I see the table entity's data in a Json format but it cannot be deserialized into the target object implementing ITableEntity
. What am I missing in this excercise?
Upvotes: 0
Views: 119
Reputation: 11383
To get table from Azure Storage and by using ITableEntity
, you can use below code which worked for me:
Function1.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"
}
}
Table in Storage Account:
Output:
Upvotes: 0