Home > Net >  Azure Function fails to execute with Table binding: connection refused
Azure Function fails to execute with Table binding: connection refused

Time:08-19

I'm learning about Azure Functions and am trying to post an object using Azure's Table binding. Here is the code for my Azure function:

[FunctionName("Table_CreateTodo")]
        public static async Task<IActionResult> Post(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "todo")]
                HttpRequest req,
            [Table("todos", Connection ="AzureWebJobsStorage")]
                IAsyncCollector<TodoTableEntity> todoTable,
            ILogger log)
        {

            try
            {
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                log.LogInformation($"Request Body: \n {requestBody}");
                var todoCreate = JsonConvert.DeserializeObject<TodoCreateModel>(requestBody);
                var todo = new Todo() { TaskDescription = todoCreate.TaskDescription };
                await todoTable.AddAsync(todo.ToTableEntity());
                return new OkObjectResult(todo);

            }
            catch (Exception ex)
            {
                return new BadRequestObjectResult(ex.ToString());
            }

        }

When I submit a POST request to the route via POSTMAN the .NET console spits out the following exception:


[2022-08-19T06:09:00.618Z] Request Body: 
[2022-08-19T06:09:00.618Z]  {"TaskDescription": "AZURREEE"}
[2022-08-19T06:09:07.492Z] Executed 'Table_CreateTodo' (Failed, Id=334b1470-eda9-47b7-91a2-4766ba7f98b2, Duration=7035ms)
[2022-08-19T06:09:07.493Z] System.Private.CoreLib: Exception while executing function: Table_CreateTodo. Microsoft.Azure.WebJobs.Host: Error while handling parameter todoTable after function returned:. Azure.Core: Retry failed after 4 tries. Retry settings can be adjusted in ClientOptions.Retry. (Connection refused (127.0.0.1:10002)) (Connection refused (127.0.0.1:10002)) (Connection refused (127.0.0.1:10002)) (Connection refused (127.0.0.1:10002)). Azure.Core: Connection refused (127.0.0.1:10002). System.Net.Http: Connection refused (127.0.0.1:10002). System.Net.Sockets: Connection refused.

I can confirm that there are no processes running on port 10002 so I'm not sure what the issue is. I can confirm that all Azure functions that don't use the Table bindings are working.

Here is my local.settings.json file:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureWebJobsSecretStorageType": "files"
  }
}

From my understanding the value of AzureWebJobsStorage will use the local table storage so I don't need to provide the connection string to an actual db.

Here is my .csproj file:

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

Any help would be really appreciated! I tried running the example from Azure's table binding example here, but I'm getting the same exception as above:

System.Private.CoreLib: Exception while executing function: Table_CreateTodo. Microsoft.Azure.WebJobs.Host: Error while handling parameter todoTable after function returned:. Azure.Core: Retry failed after 4 tries. Retry settings can be adjusted in ClientOptions.Retry. (Connection refused (127.0.0.1:10002)) (Connection refused (127.0.0.1:10002)) (Connection refused (127.0.0.1:10002)) (Connection refused (127.0.0.1:10002)). Azure.Core: Connection refused (127.0.0.1:10002). System.Net.Http: Connection refused (127.0.0.1:10002). System.Net.Sockets: Connection refused.

CodePudding user response:

Your connection string is using local storage and you havent started docker or npm.

You can start it

docker run -p 10000:10000 -p 10001:10001 -p 10002:10002 \
    mcr.microsoft.com/azure-storage/azurite

or npm

npm install -g azurite
azurite --silent --location c:\azurite --debug c:\azurite\debug.log
  • Related