我使用 HTTP 触发器来触发 Azure 函数。 我的请求正文只有 30MB 左右,URL 长度低于 4KB。 该函数仍然正常运行,但是当返回响应或抛出异常时。我会收到错误消息,但永远不会收到响应(状态代码始终为 500)。如果主体大小在 25MB 左右或小于 25MB,该功能可以正常工作并且仍然收到响应。
这是错误消息:
Failed to proxy request to the worker. Retrying in 50ms. Attempt 1 of 3. Failed to proxy request to the worker. Retrying in 100ms. Attempt 2 of 3. System.Private.CoreLib: Exception while executing function: Functions.function-name. Microsoft.Azure.WebJobs.Script.Grpc: Failed to proxy request with ForwarderError: RequestBodyDestination. One or more er rors occurred. (Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host..) (An error occurred while sending the request.). System.Net.Sockets: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.. An existing connection was forcibly closed by the remote host.
我希望能够根据 Azure Function 文档处理正文大小小于 100MB 的请求,而不会再遇到这样的错误。
当您在独立函数中使用 ASP .NET Core 集成时,该错误将分别在program.cs 和 .csproj 文件中包含 ConfigureFunctionsWebApplication() 及其相关包。
Azure 隔离功能带有或不带集成。如果您习惯在不集成 ASP .NET Core 的情况下使用独立的 Azure Function,那么请使用 ConfigureFunctionsWorkerDefaults()。您应该在program.cs文件中给出代码。
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.Build();
host.Run();
为了支持这一点,您应该在 .csproj 文件中提供包。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>_79181186</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.0" />
</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>
我对对我有用的函数文件进行了以下更改。
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using System.Net;
namespace _79181186
{
public class TestFunction
{
private readonly ILogger<TestFunction> _logger;
public TestFunction(ILogger<TestFunction> logger)
{
_logger = logger;
}
[Function("test-function")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
try
{
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteStringAsync("Request processed successfully.");
return response;
}
catch (Exception ex)
{
var errorResponse = req.CreateResponse(HttpStatusCode.BadRequest);
await errorResponse.WriteStringAsync($"An error occurred: {ex.Message}");
return errorResponse;
}
}
}
}
我发送的请求正文大小为 30MB。
Azure Functions Core Tools
Core Tools Version: 4.0.6610 Commit hash: N/A +0d55b5d7efe83d85d2b5c6e0b0a9c1b213e96256 (64-bit)
Function Runtime Version: 4.1036.1.23224
[2024-11-14T08:37:36.560Z] Found C:\Users\*****\79181186\79181186.csproj. Using for user secrets file configuration.
[2024-11-14T08:37:40.597Z] Azure Functions .NET Worker (PID: 6272) initialized in debug mode. Waiting for debugger to attach...
[2024-11-14T08:37:40.664Z] Worker process started and initialized.
Functions:
test-function: [POST] http://localhost:7256/api/test-function
For detailed output, run func with --verbose flag.
[2024-11-14T08:37:50.923Z] Executing 'Functions.test-function' (Reason='This function was programmatically called via the host APIs.', Id=72b320ab-75fd-44a6-a513-be5742ae2c08)
[2024-11-14T08:37:51.486Z] C# HTTP trigger function processed a request.
[2024-11-14T08:37:51.548Z] Executed 'Functions.test-function' (Succeeded, Id=72b320ab-75fd-44a6-a513-be5742ae2c08, Duration=1229ms)
同样的方法也适用于门户网站。