c# - Azure Function 无法使用表绑定(bind)执行 : connection refused

标签 c# .net azure azure-functions

我正在学习 Azure Functions,并尝试使用 Azure 的表绑定(bind)发布对象。这是我的 Azure 函数的代码:

[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());
            }

        }

当我通过 POSTMAN 向路由提交 POST 请求时,.NET 控制台会抛出以下异常:


[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.

我可以确认端口 10002 上没有正在运行的进程,因此我不确定问题是什么。我可以确认所有不使用表绑定(bind)的 Azure 函数都可以正常工作。

这是我的 local.settings.json 文件:

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

根据我的理解,AzureWebJobsStorage 的值将使用本地表存储,因此我不需要提供到实际数据库的连接字符串。

这是我的 .csproj 文件:

<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>

任何帮助将不胜感激!我试过running the example from Azure's table binding example here ,但我得到了与上面相同的异常:

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.

最佳答案

您的连接字符串正在使用本地存储,而您尚未启动docker或 npm。

你可以启动它

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

或 npm

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

关于c# - Azure Function 无法使用表绑定(bind)执行 : connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73412660/

相关文章:

azure - 升级 Service Fabric 应用程序

c# - 调整内容大小以填充窗口的 `Client Area`

c# - EF Core 中 SqlFunctions 的等效项

.net - 推荐用于 .NET 的策略游戏引擎?

c# - 错误消息 'The specified locale is not supported on this operating system.'

azure - 续订 Azure Key Vault 中存储的证书的最佳替代方法是什么?

c# - 是否可以从 FileResult 获取 byte[]?

c# - 将TextBlock的文本设置为字符串加绑定(bind)项?

.net - 如何注册通用对象工厂?

java - Azure spring应用程序部署,找不到 Controller 404