11

我是 Durable 功能(编排功能)的新手,并且根据 Microsoft 文档查看了示例应用程序。所以我几乎没有疑问。

例子:

public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, methods: "post", 
            Route = "orchestrators/{functionName}")] HttpRequestMessage req,
            [OrchestrationClient] DurableOrchestrationClient starter,
            string functionName,
            TraceWriter log)
        {
            // Function input comes from the request content.
            dynamic eventData = await req.Content.ReadAsAsync<object>();
            string instanceId = await starter.StartNewAsync(functionName, eventData);

            log.Info($"Started orchestration with ID = '{instanceId}'.");

            return starter.CreateCheckStatusResponse(req, instanceId);
        } 

为了调用它,我使用邮递员发出了 HTTP POST 请求,因此请求成功处理,但是当我配置不同的动词(如 HTTP GET)时,它在控制台中以 NotFound 错误响应,并且来自浏览器的 HTTP 请求向它发出的请求以“NotFound”错误响应在控制台中。为什么会这样?

我可以使用计时器触发天蓝色功能调用任何编排功能吗?

如果不是为什么?

更新:

有关问题的一些其他详细信息

    [FunctionName("TimerTrigger")]
            public static async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
            {//this runs for every 5minutes
                using (HttpClient client = new HttpClient())
                {
                    var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("", "")
                });
               //making request to above function by http trigger
                    var result = await client.PostAsync("http://localhost:7071/orchestrators/E1_HelloSequence", content);
                }
                log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
                return;
            }

我可以通过计时器触发对 http 触发器的请求吗,为什么因为我的持久函数有长时间运行的过程,所以如果在计时器触发器本身中调用编排函数,那么可能有计时器触发超时的可能性,所以我为什么要尝试遵循这种方法。是可以通过上面的代码调用吗?

4

4 回答 4

10

这是一般的 Azure Functions 行为。GET 不起作用的原因是示例中的函数仅配置为与 POST 一起使用。查看[HttpTrigger]函数签名中的属性:

[HttpTrigger(AuthorizationLevel.Anonymous, methods: "post", 
        Route = "orchestrators/{functionName}")]

如果要支持 GET,则methods相应地更改参数:

[HttpTrigger(AuthorizationLevel.Anonymous, methods: "get", 
        Route = "orchestrators/{functionName}")]

请注意,Visual Studio 似乎有一个缓存错误,即在本地调试时未正确保存对路由信息的更改。我在这里打开了一个 GitHub 问题来跟踪它:https ://github.com/Azure/Azure-Functions/issues/552

有关 HTTP 触发器的更多信息,请参阅此文档:https ://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook

如果您想使用计时器触发器来触发持久功能,那么只需更改触发器类型即可。例如:

[FunctionName("ScheduledStart")]
public static async Task RunScheduled(
    [TimerTrigger("0 0 * * * *")] TimerInfo timerInfo,
    [OrchestrationClient] DurableOrchestrationClient starter,
    TraceWriter log)
{
    string functionName = "E1_HelloSequence";
    string instanceId = await starter.StartNewAsync(functionName, null);
    log.Info($"Started orchestration with ID = '{instanceId}'.");
}

编辑:如果您使用的是 Durable v2.x,则语法如下所示:

[FunctionName("ScheduledStart")]
public static async Task RunScheduled(
    [TimerTrigger("0 0 * * * *")] TimerInfo timerInfo,
    [DurableClient] IDurableClient starter,
    ILogger log)
{
    string functionName = "E1_HelloSequence";
    string instanceId = await starter.StartNewAsync(functionName, null);
    log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
}

有关计时器触发器的更多信息,请参阅此文档:https ://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer

于 2017-10-17T22:19:46.990 回答
7

当我配置不同的动词(如 HTTP GET)时,它在控制台中以 NotFound 错误响应,而来自浏览器的 http 请求向它发出的请求在控制台中以“NotFound”错误响应。为什么会发生这种情况?

因为您指定仅在 POST 上触发您的函数:

[HttpTrigger(AuthorizationLevel.Anonymous, methods: "post", 
Route = "orchestrators/{functionName}")] HttpRequestMessage req,

要启用 GET,请添加getmethods参数。

我可以使用计时器触发天蓝色功能调用任何编排功能吗?

OrchestrationClient您可以使用类似于 HTTP 函数的输入绑定来定义计时器触发函数。样本声明:

public static async Task Run(
    [TimerTrigger("0 */1 * * * *")] TimerInfo info,
    [OrchestrationClient] DurableOrchestrationClient starter)
于 2017-10-17T19:06:18.187 回答
0

更多信息:

“IDurableClient”是“IDurableEntityClient”和“IDurableOrchastrationClient”的联合。它具有其他两个的所有方法。

实体方法:

CleanEntityStorageAsync(Boolean, Boolean, CancellationToken)
ListEntitiesAsync(EntityQuery, CancellationToken)
ReadEntityStateAsync(EntityId, String, String)
SignalEntityAsync(EntityId, DateTime, String, Object, String, String)
SignalEntityAsync(EntityId, String, Object, String, String)
SignalEntityAsync( EntityId, Action)
SignalEntityAsync(EntityId, DateTime, Action)
SignalEntityAsync(String, Action)
SignalEntityAsync(String, DateTime, Action)

编排方法:

CreateCheckStatusResponse(HttpRequest, String, Boolean)
CreateCheckStatusResponse(HttpRequestMessage, String, Boolean) CreateHttpManagementPayload(String)
GetStatusAsync(String, Boolean, Boolean, Boolean) ListInstancesAsync(OrchestrationStatusQueryCondition, CancellationToken)
PurgeInstanceHistoryAsync(DateTime, Nullable, IEnumerable)
PurgeInstanceHistoryAsync(String)
RaiseEventAsync(字符串,字符串,对象)
RaiseEventAsync(字符串,字符串,字符串,对象,字符串)
RestartAsync(字符串,布尔值)
StartNewAsync(字符串,字符串)
StartNewAsync(字符串,字符串,T)
StartNewAsync(字符串,T)
TerminateAsync(字符串,字符串)
WaitForCompletionOrCreateCheckStatusResponseAsync(HttpRequest, String, Nullable, Nullable, Boolean)
WaitForCompletionOrCreateCheckStatusResponseAsync(HttpRequestMessage, String, Nullable, Nullable, Boolean)

https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.webjobs.extensions.durabletask?view=azure-dotnet

于 2021-03-12T14:30:39.130 回答
0

Microsoft Docs 为“永恒的工作”编排器提供了一个示例,以编排需要定期但永远完成的工作:https ://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions -eternal-orchestrations?tabs=csharp#periodic-work-example

 [FunctionName("EternalWorkOrchestrator")]
 public static async Task Run([OrchestrationTrigger] DurableOrchestrationContext context)
 {
    await context.CallActivityAsync("DoWork", null);

    // sleep for one hour between doing work
    DateTime nextJobStart = context.CurrentUtcDateTime.AddHours(1);
    await context.CreateTimer(nextJobStart, CancellationToken.None);

    context.ContinueAsNew(null);
 }
于 2021-11-05T20:23:19.350 回答