HTTP 触发的 Azure Function 在手动触发时运行速度很快,但使用 Azure 逻辑应用 HTTP 时会超时

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

使用 Azure 逻辑应用 HTTP 操作调用 Azure 函数时出现此错误:

Http请求失败:服务器在超时限制内没有响应。请参阅逻辑应用限制:https://aka.ms/logic-apps-limits-and-config#http-limits

我知道等待函数响应的时间有 230 秒的限制。但是,当使用 Python 触发时,该函数会在大约 150 秒内返回输出。

url = 'XXX' # Azure Function url
data = {
  "Connect-Type": "application/json"
}
r = requests.post(url, data=json.dumps(data), headers={'Content-Type': 'application/json'})

有人知道可能出了什么问题吗?或者有没有办法让函数在限制内“响应”,并继续运行?

python azure azure-functions azure-logic-apps
1个回答
0
投票

独立函数在使用最新的库时确实可以工作。

  • 我还使用以下命令创建了该函数 -
 - func init LocalFunctionProj --worker-runtime dotnet-isolated --target-framework net8.0
 - cd LocalFunctionProj
 - func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous"
  • 它创建所有与功能相关的文件,其中包含以下代码。

HttpExample.cs-

using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace LocalFunctionProj
{
    public class HttpExample
    {
        private readonly ILogger _logger;

        public HttpExample(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<HttpExample>();
        }

        [Function("HttpExample")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            response.WriteString("Welcome to Azure Functions!");

            return response;
        }
    }
}

local.settings.json-

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
    }
}

程序.cs-

using Microsoft.Extensions.Hosting;

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

host.Run();

host.json-

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            },
            "enableLiveMetricsFilters": true
        }
    }
}
  • 更新软件包后.csproj如下所示-
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.3-preview1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>
  • 现在,当我执行该函数时,我得到了预期的响应。

enter image description here

  • 如果您仍然没有得到输出,我建议您尝试从头开始创建该函数。
© www.soinside.com 2019 - 2024. All rights reserved.