我目前为 Azure 函数编写的代码面临一个非常奇怪的问题。它在 UAT 环境中运行良好,并且在使用 local.settings.json 连接生产环境时也可以在本地运行。但部署到实际生产环境时,编排器却无法运行。没有错误或警告消息,即使我添加了额外的日志记录和 try-catch 块,但日志中没有显示任何内容。
更不寻常的是,并非所有协调者都会失败;有些有效,有些则无效,在比较它们时我没有发现任何明显的模式。例如,其中一个失败的协调器使用以下代码:
var instanceId = await starter.StartNewAsync(nameof(Orchestrator1), $"{name}-{Guid.NewGuid()}.{fileExt}",
new {
FileName = $"{name}.{fileExt}",
QuoteNumber = quoteNumber
});
Orchestrator1 根本不会被触发。我已经验证所有涉及的变量都是有效且可检索的。我还确认了 local.settings.json 文件与生产环境的配置完全匹配。当前运行时是.net6,安装的包是“Microsoft.Azure.WebJobs.Extensions.DurableTask”版本2.11.1
任何人都可以帮助我理解为什么会发生这种情况吗?什么可能导致它在本地(使用生产环境设置)和 UAT 中工作,但在实际生产部署中失败?
使用下面的代码对我有用:
[Function("Function1_HttpStart")]
public static async Task<HttpResponseData> HttpStart(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
[DurableClient] DurableTaskClient ricl,
FunctionContext executionContext)
{
ILogger ri_lg = executionContext.GetLogger("Function1_HttpStart");
string rith_bd = await new StreamReader(req.Body).ReadToEndAsync();
var rith_val = JsonSerializer.Deserialize<JsonElement>(rith_bd);
string peru = rith_val.GetProperty("name").GetString() ?? "default";
string extu = rith_val.GetProperty("fileExt").GetString() ?? "txt";
string ri_qn = rith_val.GetProperty("quoteNumber").GetString() ?? "000";
var ri_in = new
{
FileName = $"{peru}.{extu}",
QuoteNumber = ri_qn
};
ri_lg.LogInformation("Hello Rithwik Bojja, Starting orchestration with FileName: {FileName}, QuoteNumber: {QuoteNumber}", ri_in.FileName, ri_qn);
string rid = await ricl.ScheduleNewOrchestrationInstanceAsync(
nameof(Function1),
ri_in);
ri_lg.LogInformation("Hello Rithwik Bojja, Started orchestration with ID = '{instanceId}'.", rid);
return await ricl.CreateCheckStatusResponseAsync(req, rid);
}
这里我使用了 .net 6 的
Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.1.1"
来使其兼容。
csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.1.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.4" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.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>
输出:
本地:
然后:
部署到 Azure 后: