Durable Functions:如何将参数传递给 Orchestrator?

PKo*_*ant 3 c# azure-durable-functions

我是 Azure Durable 函数的新手,一直在关注“Azure Serverless Computing Cookbook”一书中的示例代码,但我被困住了,因为我的 Orchestrator 中的 .GetInput 函数返回 null。我的 Blob 触发器将文件名作为参数传递给我的 Orchestrator。我认为它调用了错误的重载函数,但不确定如何调用正确的函数。

await starter.StartNewAsync("CSVImport_Orchestrator", name); 
Run Code Online (Sandbox Code Playgroud)
        [FunctionName("CSVImport_Orchestrator")]
        public static async Task<List<string>> RunOrchestrator([OrchestrationTrigger] IDurableOrchestrationContext context)
        {
            var outputs = new List<string>();
            string CSVFileName = context.GetInput<string>(); //<<== returns null???
            {
                List<Employee> employees = await context.CallActivityAsync<List<Employee>>("ReadCSV_AT", CSVFileName);
            }
            return outputs;
        }

        [FunctionName("CSVImportBlobTrigger")]
        public static async  void Run([BlobTrigger("import-obiee-report/{name}", Connection = "StorageConnection")]Stream myBlob, string name, [DurableClient]IDurableOrchestrationClient starter, ILogger log)
        {
            string instanceId = await starter.StartNewAsync("CSVImport_Orchestrator", name); 
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
        }
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助。

pin*_*x33 6

您正在调用非通用重载,StartAsync(string, string)其中第二个string参数表示 InstanceId 而不是输入参数。还有一个通用重载,其中第二个参数代表数据。您正在传递一个stringso 重载决议看到两个潜在的候选人。然后它更喜欢非通用的,因为它是完全匹配的,因此“丢失”了您的数据。

如果您确实需要string输入数据,则需要显式指定泛型参数以强制编译器选择正确的重载:

await starter.StartAsync<string>("CSVImport_Orchestrator", name);
Run Code Online (Sandbox Code Playgroud)

现在,文档还指出输入应该是 JSON-serializeable object。从技术上讲string是,但我不确定它如何与编排器的序列化程序一起使用。您可以改为传递包含您的数据的类。这样做的好处是可以正确推断出通用参数:

public class Data {
   public string Name { get; set; } 
} 

// calling 
await starter.StartAsync("CSVImport_Orchestrator", new Data { Name = name });

// using 
var csvFileName = context.GetInput<Data>()?.Name;
Run Code Online (Sandbox Code Playgroud)