在 Visual Studio Code 中本地使用 Dot Net 8 的 Azure Function 面临问题

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

我使用 Visual Studio Code 在 Azure 中创建了一个 C# 函数,并按照以下步骤操作

  1. 为您的函数项目选择一种语言 --> 选择 C#。
  2. 选择 .NET 运行时 --> 选择 .NET 8.0 隔离 (LTS)。
  3. 为项目的第一个功能选择一个模板 --> 选择 HTTP 触发器。1
  4. 提供函数名称 Type --> HttpExample。
  5. 提供命名空间类型 --> My.Functions。
  6. 授权级别 --> 选择匿名。
  7. 打开您的项目 --> 选择在当前窗口中打开。

在此步骤之后,我构建并尝试在本地运行该应用程序, 我收到错误:

dotnet run
Unhandled exception. System.InvalidOperationException: The gRPC channel URI 'http://:' could not be parsed.
   at Microsoft.Extensions.DependencyInjection.GrpcServiceCollectionExtensions.GetFunctionsHostGrpcUri(IConfiguration configuration) in D:\a\_work\1\s\src\DotNetWorker.Grpc\GrpcServiceCollectionExtensions.cs:line 97

enter image description here

如何解决这个问题?

我对 .NET 6.0isolated (LTS) 尝试了相同的步骤。版本其工作和运行完美。 我还更新并安装了 .net 版本 8 的相关必需工具和软件包更新,但仍然面临错误。

c# .net asp.net-core .net-core azure-functions
1个回答
0
投票

我看到您正在尝试使用

dotnet run
命令运行 Azure 隔离函数,但您需要使用
func host start
func start
命令来运行 Azure 函数。

我使用 Visual Studio 代码中的模板创建了一个默认的 .Net8 隔离函数,我的函数代码如下所示 -

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public class HttpTrigger1
    {
        private readonly ILogger<HttpTrigger1> _logger;

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

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

要运行该函数,请在 Visual Studio 代码中导航至 运行 -> 开始调试或运行而不调试。您还可以在终端中使用 func host start 或 func start 命令执行您的函数,如下所示 -

C:\Users\********\Documents\functionApp\78437173> func host start
MSBuild version 17.9.8+b34f75857 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  78437173 -> C:\Users\*******\Documents\functionApp\78437173\bin\output\78437173.dll
  Determining projects to restore...
  Restored C:\Users\*******\AppData\Local\Temp\oclw0pui.c1o\WorkerExtensions.csproj (in 456 ms).
  WorkerExtensions -> C:\Users\******\AppData\Local\Temp\oclw0pui.c1o\buildout\Microsoft.Azure.Functions.Worker.Extensions.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.00



Azure Functions Core Tools
Core Tools Version:       4.0.5530 Commit hash: N/A +c8883e7f3c06e2b424fbac033806c19d8d91418c (64-bit)
Function Runtime Version: 4.28.5.21962

[2024-05-06T15:19:21.268Z] Found C:\Users\********\Documents\functionApp\78437173\78437173.csproj. Using for user secrets file configuration.
[2024-05-06T15:19:23.031Z] Worker process started and initialized.

Functions:

        HttpTrigger1: [GET,POST] http://localhost:7071/api/HttpTrigger1

For detailed output, run func with --verbose flag.
[2024-05-06T15:19:27.972Z] Host lock lease acquired by instance ID '000000000000000000000000BF6D1ED5'.
[2024-05-06T15:19:36.617Z] Executing 'Functions.HttpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=8a2b699d-77c1-4d02-8cbf-065df810e03f)
[2024-05-06T15:19:36.960Z] C# HTTP trigger function processed a request.
[2024-05-06T15:19:36.976Z] Executing OkObjectResult, writing value of type 'System.String'.
[2024-05-06T15:19:37.097Z] Executed 'Functions.HttpTrigger1' (Succeeded, Id=8a2b699d-77c1-4d02-8cbf-065df810e03f, Duration=476ms)
© www.soinside.com 2019 - 2024. All rights reserved.