azure-functionsazure-durable-functionsorchestration

Passing Parameter from Http Trigger to Orchestration function


I am developing an Azure Durable Function using .NET 7. The HttpTrigger receives a payload in the POST method.

[Function("CreateCandidateProfileOrchestration_HttpStart")]
public static async Task<HttpResponseData> HttpStart(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
    [DurableClient] DurableTaskClient client,
    FunctionContext executionContext)
{
}

The payload should be passed to Orchestration method as parameter:

[Function(nameof(CreateCandidateProfileOrchestration))]
public static async Task<List<string>> RunOrchestrator(
    [OrchestrationTrigger] TaskOrchestrationContext context)
{
}

What would be a standard way to achieve that?

Any example code would be appreciated.

Thanks. Ruhul


Solution

  • I tried the code below to pass a parameter from an Http Trigger to the Orchestration function.

    Code :

    using System.Collections.Generic;
    using System.IO;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text.Json;
    using System.Threading.Tasks;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.Azure.WebJobs.Extensions.DurableTask;
    using Microsoft.Extensions.Logging;
    
    public class MyPayloadType
    {
        public string Property1 { get; set; }
        public int Property2 { get; set; }
    }
    
    public static class CreateCandidateProfileOrchestration
    {
        [FunctionName("CreateCandidateProfileOrchestration_HttpStart")]
        public static async Task<HttpResponseMessage> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestMessage req,
            [DurableClient] IDurableOrchestrationClient client,
            ILogger log)
        {
            string requestBody = await req.Content.ReadAsStringAsync();
            var payload = JsonSerializer.Deserialize<MyPayloadType>(requestBody);
            string instanceId = await client.StartNewAsync("CreateCandidateProfileOrchestration", payload);
            log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
    
            var response = new HttpResponseMessage();
            var content = new JsonContent(new List<string> { "Started orchestration." });
            response.Content = content;
            return response;
        }
    
        [FunctionName(nameof(CreateCandidateProfileOrchestration))]
        public static async Task<List<string>> RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            ILogger log)
        {
            var payload = context.GetInput<MyPayloadType>();
            log.LogInformation($"Received payload: {payload.Property1}, {payload.Property2}");
    
            return new List<string> { "Orchestration result" };
        }
    
        [FunctionName("ActivityFunction")]
        public static async Task<string> ActivityFunction(
            [ActivityTrigger] string input, ILogger log)
        {
            return $"Activity completed with input: {input}";
        }
    }
    
    public class JsonContent : StringContent
    {
        public JsonContent(object value) : base(Newtonsoft.Json.JsonConvert.SerializeObject(value))
        {
            Headers.ContentType = new MediaTypeHeaderValue("application/json");
        }
    }
    

    Output in Postman:

    Follow these steps to get the output in Postman:

    URL:

    http://localhost:7260/api/CreateCandidateProfileOrchestration_HttpStart
    

    JSON body:

    {
      "Property1": "Value1",
      "Property2": 42
    }
    

    Output data in Postman:

    [
        "Started orchestration."
    ]
    

    Enter image description here

    Terminal Output:

    I received the payload details in the output below.

    Functions:
    
            CreateCandidateProfileOrchestration_HttpStart: [POST] http://localhost:7260/api/CreateCandidateProfileOrchestration_HttpStart
    
            ActivityFunction: activityTrigger
    
            CreateCandidateProfileOrchestration: orchestrationTrigger
    
    For detailed output, run func with --verbose flag.
    [2024-01-24T06:07:46.885Z] Host lock lease acquired by instance ID '0000000xxxxxxxxxxxxxxxx'.
    [2024-01-24T06:08:06.178Z] Executing 'CreateCandidateProfileOrchestration_HttpStart' (Reason='This function was programmatically called via the host APIs.', Id=898a0b0cxxxxxxxxxxxxxxxxxx)
    [2024-01-24T06:08:06.392Z] Started orchestration with ID = '94db3fxxxxxxxxxxxxxxxx'.
    [2024-01-24T06:08:06.431Z] Executed 'CreateCandidateProfileOrchestration_HttpStart' (Succeeded, Id=898a0b0cxxxxxxxxxxxxxx, Duration=310ms)
    [2024-01-24T06:08:06.563Z] Executing 'CreateCandidateProfileOrchestration' (Reason='(null)', Id=0011bc56xxxxxxxxxxxxxx)
    [2024-01-24T06:08:06.604Z] Received payload: Value1, 42
    [2024-01-24T06:08:06.616Z] Executed 'CreateCandidateProfileOrchestration' (Succeeded, Id=0011bc56xxxxxxxxxxxxxx, Duration=67ms)
    

    enter image description here