c# - 隔离功能应用程序不响应 HTTP 调用

标签 c# azure azure-functions azure-functions-isolated

我之前使用过进程内函数应用,没有任何问题。

我使用 func host start 启动它们,然后我可以使用 cURL 在本地调用任何 REST 端点。

为了跟上更高版本的 dotnet,我决定尝试一下隔离运行时。

我设置了一个非常简单的项目,仅公开一个端点,如 documentation for the http trigger 中所述。

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

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

    [Function(nameof(Run))]
    public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestData req,
        FunctionContext executionContext)
    {
        _logger.LogInformation("message logged");

        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
        response.WriteString("Welcome to .NET isolated worker !!");

        return response;
    }
}

Program.cs中我有基线设置。从我读到的内容来看,我看不出这种情况需要任何额外的东西:

using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .Build();

host.Run();

我启动此应用程序的方式与使用 func host start 在进程中启动的方式相同。它启动正常并将我的端点列出为:

Run: [GET] http://localhost:7071/api/Run

但是,当我尝试使用 cURL 调用此函数时,调用只是挂起。我可以看到已达到运行时的日志输出,但无法从端点获得响应:

[2023-08-22T09:48:28.362Z] Executing 'Functions.Run' (Reason='This function was programmatically called via the host APIs.', Id=98b63dcc-dce5-4b9d-bee8-dee1ea98a653)

在我的 csproj 文件中,我有以下依赖项:

<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.8.0"/>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />

我正在使用 Azure Function Core Tools 4.46.0

我在这里缺少什么?

尝试 Debug模式

正如我所指出的,我也尝试了 Debug模式,但得到了相同的结果:

func host start --pause-on-error --dotnet-isolated-debug --enable-json-output

Azure Functions Core Tools
Core Tools Version:       4.0.5274 Commit hash: N/A  (64-bit)
Function Runtime Version: 4.23.0.20886

[2023-08-22T11:34:02.028Z] Found MyProject.csproj. Using for user secrets file configuration.
[2023-08-22T11:34:04.234Z] Azure Functions .NET Worker (PID: 70640) initialized in debug mode. Waiting for debugger to attach...

Functions:

    Run: [GET] http://localhost:7071/api/Run

For detailed output, run func with --verbose flag.
[2023-08-22T11:34:09.120Z] Host lock lease acquired by instance ID '000000000000000000000000C146DC40'.
[2023-08-22T11:34:10.565Z] Worker process started and initialized.
[2023-08-22T11:35:12.462Z] Executing 'Functions.Run' (Reason='This function was programmatically called via the host APIs.', Id=2a8207da-dc97-447e-b9de-60facda8481c)

上面输出中的最后一行是我使用 cURL 尝试调用端点,然后什么也没有发生。

最佳答案

我首先使用以下值更新您的 .csproj 依赖项:

<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.18.0"/>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.13.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />

您还可以尝试按照 here. 将输出类型更改为 ASP.NET Core 类型。

关于c# - 隔离功能应用程序不响应 HTTP 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76952125/

相关文章:

node.js - 如何在Azure中使用Node JS功能上传文件

c# - Linq to XML 选择所有父项和子项

c# - 具有许多扩展的 OpenFileDialog

c# - 选择两个列表中常见的元素哪个更有效?

c# - 类 CSS3 框阴影实现/算法

Azure VM 捕获(流程概述)

没有公共(public) DNS 的 Azure VM

azure - 从 Azure Functions 访问 Azure Key Vault 时访问被拒绝

python - Azure 事件中心与 Python 中的 Apache Spark 集成

c# - Azure Function 中的自定义绑定(bind)未得到解决