你好,Stack Overflow社区,
在将Azure Functions应用程序从进程内模型(.NET 6)迁移到隔离工作模型(.NET 8)后,我遇到了一个特殊问题。当从orchestrator函数通过[ActivityTrigger]传递字符串参数时,会出现此问题。
迁移详情
已从进程内(.NET 6)迁移到独立工作进程(.NET 8)。问题:从编排器调用[ActivityTrigger]函数时,字符串参数(jobId)意外地用附加引号括起来。
尝试解决:
- 浏览并调整了JSON序列化设置。
- 实现了一个自定义JSON转换器(JsonCustomConverter)来修剪引号,但有趣的是,该转换器在读取或写入操作时不会检测到这些额外的引号。
- 创建了一个独立的最小函数来复制问题,确认问题在我的主应用程序上下文之外仍然存在。
示例代码截图
public async Task<JobResult> Run([OrchestrationTrigger] TaskOrchestrationContext ctx) {
// Passing jobId (e.g., "6593e1c1e17f4767b0668150") to Activity Function
await ctx.CallActivityAsync<bool>(nameof(IsCancellationRequested), jobId);
}
[Function(nameof(IsCancellationRequested))]
public Task<bool> IsCancellationRequested([ActivityTrigger] string jobId) {
// Here, jobId is received with extra quotes: "\"6593e1c1e17f4767b0668150\""
return jobService.IsCancellationRequested(jobId);
}
字符串
下面是一个纯函数的代码:
public class Function1
{
private readonly ILogger _logger;
public Function1(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<Function1>();
}
[Function("Function1")]
public async Task<string> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
[DurableClient] DurableTaskClient starter)
{
var input = "test";
_logger.LogInformation($"Input before orchestrator: {input}");
await starter.ScheduleNewOrchestrationInstanceAsync("Orchestrator", input);
return "";
}
[Function("Orchestrator")]
public async Task<string> RunOrchestrator([OrchestrationTrigger] TaskOrchestrationContext ctx)
{
var input = ctx.GetInput<string>();
_logger.LogInformation($"Input before activity: {input}");
var test = await ctx.CallActivityAsync<string>(nameof(Test), input);
_logger.LogInformation($"Input after activity: {test}");
return test;
}
[Function(nameof(Test))]
public Task<string> Test([ActivityTrigger] string test)
{
_logger.LogInformation($"Input inside activity: {test}");
return Task.FromResult(test);
}
}
型
和程序:
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.Build();
host.Run();
型
的数据
提问
- 是什么原因导致这些额外的引号被添加到[ActivityTrigger]中的字符串参数中?
- Azure Functions(隔离工作进程,.NET 8)中的字符串处理是否存在任何可能导致此行为的已知问题?
- 我应该如何解决此问题?是否应该调查特定的配置或Azure Functions SDK方面?
1条答案
按热度按时间0sgqnhkj1#
我已经使用您的代码与以下配置,我得到预期的输出。
这里我使用
Core Tools Version: 4.0.5455 Commit hash: N/A (64-bit) and Function Runtime Version: 4.27.5.21554
。.csproj-
字符串
我在VS Code和Visual Studio中都试过你的代码。
型
输出-
型
x1c 0d1x的数据