c# - 具有 HTTP 触发器的 Azure 函数在开始处理之前返回 "received"消息

标签 c# azure azure-functions

我正在创建一个 Azure 函数来处理文件。 Azure 函数将使用 HTTP 触发器激活,因此只要站点的页面向其发出 HTTP 请求,就应执行该函数。

该函数需要一些时间才能完成文件处理,但我无法让站点等待该函数完成以了解一切是否正常。因此,我想要的是来自 Azure Function 的某种“已收到”消息,以便在开始处理之前知道它已收到 HTTP 请求。

有没有办法使用 HTTP 触发器来做到这一点?我可以让调用者知道其请求已正确接收,然后开始执行 Azure Functions 吗?

最佳答案

是的,使用 Durable Functions 可以非常轻松地做到这一点:

1-安装“Microsoft.Azure.WebJobs.Extensions.DurableTask”nuget 包;

2-

   [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [DurableClient] IDurableOrchestrationClient starter,
        ILogger log)
    {
        Guid instanceId = Guid.NewGuid();
        string x = await starter.StartNewAsync("Processor", instanceId.ToString(), null);

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

        return starter.CreateCheckStatusResponse(req, x);
    }

3-

    [FunctionName("Processor")]
    public static async Task<string> Search([OrchestrationTrigger] IDurableOrchestrationContext context)
    {
        var output= await context.CallActivityAsync<string>("DoSomething", null);

        return output;
    }



   [FunctionName("DoSomething")]
    public static async Task<string> Execute([ActivityTrigger] string termo, ILogger log)
    {
        //do your work in here
    }

在前面的代码中,我们创建了一个 Orchestrator(处理器)函数,它将启动一个执行进程 DoSomething 函数的事件。

更多信息:https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview

关于c# - 具有 HTTP 触发器的 Azure 函数在开始处理之前返回 "received"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61489946/

相关文章:

c# - .NET 中的 System.Windows.Forms.PowerStatus 类有什么异常吗

javascript - Cosmos DB 查询可以在 Data Explorer 中运行,但不能在 Node.js 中运行

python-3.x - Python 3.6.4 可以在本地连接到 Slack(通过 slackclient),但在 Azure 中则不能

c# - 如何根据范围查询结果删除Azure表实体

c# - 使用 Azure Function (.NET Core) 下载文件

c# - 如何在 UML 中表示 .NET 字典类型?

c# - IntPtr 到字节数组并返回

c# - 删除特定用户创建的所有文件

git - 如何从现有存储库导入存储库及其 pull 请求

c# - 直接在函数方法中注入(inject) DI 实例 (Azure Functions)