Durable Function - WebHook


Azure Durable Functions extends Azure Functions to enable stateful workflows in serverless environments. It orchestrates multiple functions through reliable, long-running processes using patterns like chaining and fan-out/fan-in, with built-in state management and automatic retry capabilities.

public class WebHook(IAddressRepository repository, ILogger<WebHook> logger)
{
private readonly IAddressRepository _repository = repository ?? throw new ArgumentNullException(nameof(repository));
private readonly ILogger<WebHook> _logger = logger ?? throw new ArgumentNullException(nameof(logger));

[Function(nameof(WebHook))]
public static async Task<List<string>> RunOrchestrator(
[OrchestrationTrigger] TaskOrchestrationContext context)
{
ILogger logger = context.CreateReplaySafeLogger(nameof(WebHook));
var address = context.GetInput<Address>(); // Receive `Address` as input

logger.LogInformation("Processing Address data for ID {Id}.", address!.Id);

var outputs = new List<string>();

// Call the activity function to save the address
outputs.Add(await context.CallActivityAsync<string>(nameof(NewAddress), address));

return outputs;
}

[Function(nameof(NewAddress))]
public async Task<string> NewAddress([ActivityTrigger] Address address, FunctionContext executionContext)
{
var logger = executionContext.GetLogger("WebHook_Hello");
logger.LogInformation("Adding Address to database for ID: {Id}.", address.Id);

try
{
// Add and save the Address to the database
await _repository.AddAddressAsync(address);

logger.LogInformation("Address added successfully for ID: {Id}.", address.Id);

return $"Address added with ID {address.Id}!";
}
catch (Exception ex)
{
logger.LogError("Error adding Address to database: {Message}", ex.Message);
throw; // Re-throw the exception so Durable Task logs it
}
}


[Function("WebHook_HttpStart")]
public async Task<HttpResponseData> HttpStart(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")]
HttpRequestData req,
[DurableClient] DurableTaskClient client,
FunctionContext executionContext)
{
var logger = executionContext.GetLogger("WebHook_HttpStart");

// Deserialize the 'Address' object from the HTTP request body
var requestBody = await req.ReadAsStringAsync();
var address = System.Text.Json.JsonSerializer.Deserialize<Address>(
requestBody!, new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true });

if (address == null)
{
// Return BAD REQUEST if the body is null
var badResponse = req.CreateResponse(System.Net.HttpStatusCode.BadRequest);
await badResponse.WriteStringAsync("Invalid Address object in request body.");
return badResponse;
}

// Schedule the orchestration and pass the Address object
string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(nameof(WebHook), address);

logger.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);

// Return HTTP 202 with orchestration status
return await client.CreateCheckStatusResponseAsync(req, instanceId);

}
}

 


No files yet, migration hasn't completed yet!