.net8.0 升级时,Azure HttpTrigger 函数与 Blob() 输出问题

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

我在将存储库升级到 .net8 时遇到问题。问题在于输出到 Blob 存储的 Azure HTTPTrigger 函数。我确实注意到,当我使用 Visual Studio 中的升级工具将函数从 .net6 升级到 .net8 时,它确实将 blob 函数名称从 Blob() 更改为 BlobInput(),我认为这是不正确的,因为我输出到斑点。

在.net6版本中,该函数如下所示: enter image description here

在网上搜索时,我发现我需要安装 Microsoft.Azure.Functions.Worker.Extensions.Storage 包(我正在运行 dotnet-isolated)。

我假设我需要将 Blob() 更改为 BlobOutput() ,它只有一个路径参数,所以我需要以另一种方式定义连接。可能在program.cs文件中定义它或在函数本身中进行某种注入?

如果有人能指出我正确的方向,我将不胜感激!如果您需要查看其他内容,请告诉我。谢谢!

c# azure-functions azure-blob-storage .net-8.0 azure-functions-isolated
1个回答
0
投票

使用

BlobOutput()
实现 HTTP 触发器隔离 Azure 函数输出绑定,输出到 Blob 存储。

 [BlobOutput("Container/{name}-output.txt", Connection = "AzureWebJobsStorage")]

我创建了一个示例 .NET 8.0 隔离的 Azure 函数来实现输出绑定。

代码片段:

 public class Function1
 {
     private readonly ILogger<Function1> _logger;

     public Function1(ILogger<Function1> logger)
     {
         _logger = logger;
     }

     [Function("Function1")]
     [BlobOutput("test-samples-output/{name}-output.txt", Connection = "AzureWebJobsStorage")]

     public IActionResult RunStyleHttpAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
     {
         _logger.LogInformation("C# HTTP trigger function processed a request.");
         return new OkObjectResult("Welcome to Azure Functions!");
     }
 }

local.settings.json:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<Storage_Connection_String>",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
  }
}

.csproj:

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="6.6.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.0" />
  </ItemGroup>

Blob 在 Azure 存储容器中创建,如下所示:

enter image description here

文件内容:

enter image description here

控制台输出:

Functions:

        Function1: [GET,POST] http://localhost:7194/api/Function1

For detailed output, run func with --verbose flag.
[2024-12-21T05:20:34.550Z] Executing 'Functions.Function1' (Reason='This function was programmatically called via the host APIs.', Id=4cfe55c4-fde1-4d0e-9af6-89a4b55844e5)
[2024-12-21T05:20:34.949Z] C# HTTP trigger function processed a request.
[2024-12-21T05:20:34.958Z] Executing OkObjectResult, writing value of type 'System.String'.
[2024-12-21T05:20:39.440Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-12-21T05:20:40.723Z] Executed 'Functions.Function1' (Succeeded, Id=4cfe55c4-fde1-4d0e-9af6-89a4b55844e5, Duration=6217ms)
© www.soinside.com 2019 - 2024. All rights reserved.