c# - 持久函数 : How to pass a parameter to the Orchestrator?

标签 c# azure-durable-functions

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

await starter.StartNewAsync("CSVImport_Orchestrator", name); 
        [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");
        }
在此先感谢您的帮助。

最佳答案

您正在调用 non-generic overload of StartAsync(string, string) 第二个,string参数表示 InstanceId 而不是输入参数。还有一个generic overload其中第二个参数代表数据。您正在传递一个 string所以重载决议看到了两个潜在的候选人。然后它更喜欢非通用的,因为它是 精确 匹配,从而“丢失”您的数据。
如果您真的需要string对于您的输入数据,您需要明确指定泛型参数以强制编译器选择正确的重载:

await starter.StartAsync<string>("CSVImport_Orchestrator", name);
现在,文档还指出输入应该是一个 JSON 可序列化对象。技术上是 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;

关于c# - 持久函数 : How to pass a parameter to the Orchestrator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66619880/

相关文章:

c# - Lucene.Net QueryParser 抛出 IOException(读取过去的 eof)

c# - 在不破坏客户端代码的情况下将一个接口(interface)拆分为两个新接口(interface)?

c# - 控制台应用程序的帮助标志

c# - 属性 'DurableClientAttribute' 是一个 WebJobs 属性,.NET Worker(隔离进程)不支持该属性

c# - 当 wpf 中的静态属性发生变化时得到通知

c# - 伪造 GetCallingAssembly() 以指向不同的程序集

json - Azure Durable Functions我应该如何处理TaskFailedExceptionDeserializationException

Azure 函数 - 与文件相关的无法解释的存储帐户成本

azure - Azure Durable 函数中维护什么状态?

c# - 在 Azure Functions 中使用 DurableOrchestration 时,无法将参数 'orchestrationContext' 绑定(bind)到类型 DurableOrchestrationContext