Azure function v4 writing to Azure Table Storage - no changes to table and no errors

Moustafa El-Sawy 45 Reputation points
2023-09-14T16:24:18.58+00:00

I created a Azure functions (HTTP Trigger) v4 that writes data to an Azure Table storage.

Heres the code for my Azure function (C# .NET 6, non isolated):

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Tables" Version="1.2.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Table;
using System.Threading.Tasks;
using System;
public static class Function1
{
    [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
        [Table("tablename", Connection = "AzureWebJobsStorage")] IAsyncCollector<UserLogEntry> userLogsTable,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        try
        {
            // Process data...
            await userLogsTable.AddAsync(logEntry);
            log.LogInformation("Log entry added successfully.");
            return new OkObjectResult("Log entry added successfully.");
        }
        catch (Exception ex)
        {
            log.LogError(ex, "An error occurred while processing the request.");
            return new StatusCodeResult(500); // Internal Server Error
        }
    }
}
public class UserLogEntry : TableEntity
{
    public UserLogEntry(string partitionKey)
    {
        PartitionKey = partitionKey;
        RowKey = partitionKey + "-" + Guid.NewGuid().ToString();
    }
    // Data properties
}

I can test this locally using Azure Storage emulator (connecting via localhost) and can see the table "tablename" being populated with data as its being written to it. I then deployed this Azure function and set it as managed identity and gave it Table Storage contributor to my table. Everything was successful. I trigger the function from my local app and can see in the monitor its being triggered. I look at the logs and can see everything is OK, no errors. 2023-09-14T16:21:13Z [Information] Executing 'xxx' (Reason='This function was programmatically called via the host APIs.', Id=1f419d7c-67b4-4df1-b9d1-fda6a67acc5f) 2023-09-14T16:21:13Z [Information] C# HTTP trigger function processed a request. 2023-09-14T16:21:13Z [Information] Log entry added successfully. However, when checking the table, its unchanged and still empty. I dont know if its an access issue, or an issue with my function code. I am getting the "Log Entry added successfully" print statement which leads me to believe there were no errors. How do I debug the issue? I am not sure where to start looking.

Azure Table Storage
Azure Table Storage
An Azure service that stores structured NoSQL data in the cloud.
170 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,978 questions
{count} votes

Accepted answer
  1. Bruno Lucas 4,431 Reputation points MVP
    2023-09-21T11:51:16.3533333+00:00

    Hi @Moustafa El-Sawy

    Sorry for the delay. you can certainly use identity:

    https://video2.skills-academy.com/en-us/azure/azure-functions/functions-reference?tabs=blob&pivots=programming-language-csharp#connecting-to-host-storage-with-an-identity&WT.mc_id=AZ-MVP-5005174

    I just wanted first to see if the code was working with a connection string to rule out code problems. Did you only assign a role with access to the table? AzureWebJobsStorage is used by the function app to store function app related data, so, it's more complicated.

    User's image

    If I try this using "AzureWebJobsStorage" and only Table Storage contributor role it does not work. The function needs more access:

    User's image

    example of function app data controlled by AzureWebJobsStorage:

    User's image

    if I use a different storage from the one used by AzureWebJobsStorage, than roles for the table only work fine. Look like you can use the same storage but you will need to add more roles

    User's image

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.