通过URL请求向Azure持久化函数传递参数。

3

我有基本的durable function代码:

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触发器传递参数到编排程序,最终传递给由编排程序调用的活动。
目前在这段代码中,输出是Saying hello to .,因此看起来它没有通过代码传递参数,或者请求URL中的参数没有被读取。
是否有任何方法可以实现这一点?

我不完全理解这段代码,但是我不明白如何传递参数。也许这可以帮到你:https://www.c-sharpcorner.com/UploadFile/ca2535/query-string-in-Asp-Net/ - Iria
1个回答

4
您可以使用以下代码来接收姓名:
    var name = req.RequestUri.Query.Split("=")[1];

以下代码用于接收POST请求中的请求正文,无法接收GET请求中的参数。
    var content = req.Content;
    string jsonContent = content.ReadAsStringAsync().Result;

1
谢谢,我只是将参数作为简单的JSON放在请求体中传递 :) 这样就可以了! - TheBvrtosz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接