json Azure Functions Isolated Worker(.NET 8):在ActivityTrigger中向字符串添加额外的引号

7qhs6swi  于 5个月前  发布在  .NET
关注(0)|答案(1)|浏览(55)

你好,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方面?
0sgqnhkj

0sgqnhkj1#

我已经使用您的代码与以下配置,我得到预期的输出。
这里我使用Core Tools Version: 4.0.5455 Commit hash: N/A (64-bit) and Function Runtime Version: 4.27.5.21554

.csproj-

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <RootNamespace>_77750491</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.2" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.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>

字符串
我在VS Code和Visual Studio中都试过你的代码。

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.DurableTask;
using Microsoft.DurableTask.Client;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public class DurableFunctionsOrchestrationCSharp1
    {
        private readonly ILogger _logger;

        public DurableFunctionsOrchestrationCSharp1(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<DurableFunctionsOrchestrationCSharp1>();
        }
        [Function("OrchestratorFunction")]
        public async Task<string> RunOrchestrator(
            [OrchestrationTrigger] TaskOrchestrationContext context)
        {
            var input = context.GetInput<string>();
        
            _logger.LogInformation($"Input before activity: {input}");
            var test = await context.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);
        }

        [Function("HttpTriggerFunction")]
        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("OrchestratorFunction", input);

            return "";
        }
    }
}

输出-

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

[2024-01-03T09:32:29.064Z] Found C:\Users\*****\Documents\functionApp\77750491\77750491.csproj. Using for user secrets file configuration.
[2024-01-03T09:32:34.681Z] Worker process started and initialized.

Functions:

        HttpTriggerFunction: [POST] http://localhost:7071/api/HttpTriggerFunction

        OrchestratorFunction: orchestrationTrigger

        Test: activityTrigger

For detailed output, run func with --verbose flag.
[2024-01-03T09:32:39.685Z] Host lock lease acquired by instance ID '000000000000000000000000BF6D1ED5'.
[2024-01-03T09:35:06.834Z] Executing 'Functions.HttpTriggerFunction' (Reason='This function was programmatically called via the host APIs.', Id=ace50d87-0d02-4b8b-931a-001554f11dfe)
[2024-01-03T09:35:07.544Z] Input before orchestrator: test
[2024-01-03T09:35:07.544Z] Scheduling new OrchestratorFunction orchestration with instance ID '0de6f8ee11614fd5be6c303c84288369' and 6 bytes of input data.
[2024-01-03T09:35:07.814Z] Executed 'Functions.HttpTriggerFunction' (Succeeded, Id=ace50d87-0d02-4b8b-931a-001554f11dfe, Duration=1010ms)
[2024-01-03T09:35:07.902Z] Executing 'Functions.OrchestratorFunction' (Reason='(null)', Id=a5d08d46-0e5e-455d-8f5d-667a122f0f45)
[2024-01-03T09:35:08.448Z] Input before activity: test
[2024-01-03T09:35:08.544Z] Executed 'Functions.OrchestratorFunction' (Succeeded, Id=a5d08d46-0e5e-455d-8f5d-667a122f0f45, Duration=671ms)
[2024-01-03T09:35:08.653Z] Executing 'Functions.Test' (Reason='(null)', Id=7c7a2ebf-9b72-4f17-876f-1bef722675b0)
[2024-01-03T09:35:08.682Z] Input inside activity: test
[2024-01-03T09:35:08.686Z] Executed 'Functions.Test' (Succeeded, Id=7c7a2ebf-9b72-4f17-876f-1bef722675b0, Duration=37ms)   
[2024-01-03T09:35:08.738Z] Executing 'Functions.OrchestratorFunction' (Reason='(null)', Id=4d3c68e0-e806-467f-b6b7-6d2c155b3bc7)
[2024-01-03T09:35:08.745Z] Input before activity: test
[2024-01-03T09:35:08.753Z] Input after activity: test
[2024-01-03T09:35:08.765Z] Executed 'Functions.OrchestratorFunction' (Succeeded, Id=4d3c68e0-e806-467f-b6b7-6d2c155b3bc7, Duration=27ms)


x1c 0d1x的数据

相关问题