【问题标题】:Passing parameter to azure durable function via URL Request通过 URL 请求将参数传递给 azure 持久函数
【发布时间】:2021-06-21 20:41:19
【问题描述】:

我有基本的持久功能代码:

namespace dotNetDurableFunction
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<List<string>> RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context)
        {
            var outputs = new List<string>();
            string name = context.GetInput<string>();

            // Replace "hello" with the name of your Durable Activity Function.
            outputs.Add(await context.CallActivityAsync<string>("Function1_Hello", name));

            return outputs;
        }

        [FunctionName("Function1_Hello")]
        public static string SayHello([ActivityTrigger] string name, ILogger log)
        {
            log.LogInformation($"Saying hello to {name}.");
            return $"Hello {name}!";
        }

        [FunctionName("Function1_HttpStart")]
        public static async Task<HttpResponseMessage> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
            [DurableClient] IDurableOrchestrationClient starter,
            ILogger log)
        {
            //var name = await req.Content.ReadAsStringAsync();
            var name = "Bart";
            log.LogInformation(name);
            // Function input comes from the request content.
            string instanceId = await starter.StartNewAsync("Function1", name);

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

            return starter.CreateCheckStatusResponse(req, instanceId);
        }
    }
}

我想要实现的是通过传递参数name 来调用http 触发器,如下所示: http://localhost:7071/api/Function1_HttpStart?name=Josh

然后将参数从 http 触发器传递给 orchestrator,最后传递给 Orchestrator 调用的 Activity。

现在在这段代码中,输出是 Saying hello to .,所以看起来它没有通过代码传递参数,或者没有从请求 url 中读取参数。

有什么办法可以做到吗?

【问题讨论】:

标签: c# .net azure azure-functions azure-durable-functions


【解决方案1】:

您可以使用以下代码接收姓名:

    var name = req.RequestUri.Query.Split("=")[1];

以下代码用于在post请求中接收请求体,无法接收get请求中的参数。

    var content = req.Content;
    string jsonContent = content.ReadAsStringAsync().Result;

【讨论】:

  • 谢谢,我只是将请求正文中的参数作为简单的 json 传递 :) 成功了!
猜你喜欢
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
相关资源
最近更新 更多