如何将 POST 参数传递给 Durable Function,然后将此参数传递给 Timer Triggered 函数

Posted

技术标签:

【中文标题】如何将 POST 参数传递给 Durable Function,然后将此参数传递给 Timer Triggered 函数【英文标题】:How to pass a POST parameter to a Durable Function and then pass this param to a Timer Triggered function 【发布时间】:2019-01-28 16:13:27 【问题描述】:
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;

namespace mynamespace

    public static class myfuncclass
    
        [FunctionName("mydurablefunc")]
        public static async void Run([OrchestrationTrigger] DurableOrchestrationContextBase context)
        
            await context.CallActivityAsync<string>("timer", "myparam");
        

        [FunctionName("timer")]
        public static void RunTimer([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
        
            if (myTimer.IsPastDue)
            
                log.Info("Timer is running late!");
            
            log.Info($"Timer trigger function executed at: DateTime.Now");
        
    

我希望我的 Durable Function 启动另一个基于计时器的功能,该功能必须每 5 分钟重新发生一次。到目前为止一切顺利,这是我的代码。现在我希望这个活动在我使用 HTTP 调用(POST、GET 等)调用持久函数时开始(我更喜欢使用 Queue,但不知道如何做)并向它传递一个参数,然后它传递这个参数到被调用的函数。怎么样?

【问题讨论】:

我认为您不能“启动”计时器触发功能。它将始终以 cron-string 设置的间隔运行。 好的,我可以让它一直运行,但是如何传递参数给它呢? 对于计时器触发函数,最好的方法是让函数查询其他服务、队列或数据库以检索处理所需的数据。也许您应该改用 HttpTriggered 函数?见docs.microsoft.com/en-us/azure/azure-functions/… 【参考方案1】:

您不能“启动”定时器触发器。 Orchestrator 只能管理 Activity 函数,如下所示:

[FunctionName("mydurablefunc")]
public static async void Run([OrchestrationTrigger] DurableOrchestrationContextBase context)

    for (int i = 0; i < 10; i++)
    
        DateTime deadline = context.CurrentUtcDateTime.Add(TimeSpan.FromMinutes(5));
        await context.CreateTimer(deadline, CancellationToken.None);
        await context.CallActivityAsync<string>("myaction", "myparam");
    


[FunctionName("myaction")]
public static Task MyAction([ActivityTrigger] string param)

    // do something

【讨论】:

以上是关于如何将 POST 参数传递给 Durable Function,然后将此参数传递给 Timer Triggered 函数的主要内容,如果未能解决你的问题,请参考以下文章

如何将 JS 函数的引用作为参数传递给 ExternalInterface 调用?

如何将参数传递给节点js中的sql查询

如何将参数传递给节点js中的sql查询

如何使用 Retrofit 将参数传递给 POST 请求,然后序列化请求?

如何将 JSON 作为参数传递给控制器​​ .net api 中的 Post 方法

将状态作为参数传递给环处理程序?