Hi @Christian Hansen ,
Here is an example code for Durable Function and some screenshots how to call the Durable Function from the DataFactory.. Hope it will help. Thanks! :)
namespace abc
{
public static class DF_Parse
{
[FunctionName("DF_Parse_HttpStart")]
public static async Task<HttpResponseMessage> HttpStart([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req, [DurableClient] IDurableOrchestrationClient starter, ILogger log)
{
// Function input comes from the request content.
string requestBody = await req.Content.ReadAsStringAsync();
string instanceId = await starter.StartNewAsync("DF_Parse", null, requestBody);
log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
return starter.CreateCheckStatusResponse(req, instanceId);
}
[FunctionName("DF_Parse")]
public static async Task RunOrchestrator([OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
string sRequestBody = context.GetInput<string>();
List<Task<string>> contents = new List<Task<string>>();
myClass body = JsonConvert.DeserializeObject<myClass>(sRequestBody);
for (int i = 0; i < 10; i++)
{
JObject parameters = new JObject();
parameters.Add("iParam", i);
parameters.Add("myClass", JsonConvert.SerializeObject(myClass));
contents.Add(context.CallSubOrchestratorAsync<string>("DF_Parse_AnotherFunctionAsync", JsonConvert.SerializeObject(parameters)));
}
await Task.WhenAll(contents);
/* you can also check and work further with the response on each call if you want
//create a dictionary object from the responses
foreach (Task<string> eachTask in contents)
{
if (eachTask.IsCompletedSuccessfully)
{
record = ParseJsonString(eachTask.Result, log);
if (record.Count > 0)
{
totalRecords.Add(record);
}
}
}
*/
}
catch (Exception ex)
{
log.LogError(ex, "DF_Parse failed!");
throw ex;
}
}
[FunctionName("DF_Parse_AnotherFunctionAsync")]
public static async Task AnotherFunctionAsync([OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
{
try
{
string input = context.GetInput<string>();
List<string> list = new List<string>();
list = await context.CallActivityAsync<List<string>>("DF_Parse_ActivityFunction", input);
}
catch (Exception ex)
{
log.LogInformation(ex.Message);
throw ex;
}
}
[FunctionName("DF_Parse_ActivityFunction")]
public static async Task<List<string>> ActivityFunction([ActivityTrigger] string input, ILogger log)
{
log.LogInformation($"ActivityFunction() started...");
Dictionary<string, dynamic> parameters;
List<string> outputList = new List<string>();
try
{
parameters = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(input);
//function body
log.LogInformation("Successful!");
}
catch (Exception ex)
{
log.LogInformation($"Failed with Error: {ex.Message}");
throw ex;
}
return outputList;
}
}
}
//host.json file
{
"version": "2.0",
"extensions": {
"durableTask": {
"hubName": "ParseTaskQueue",
"maxConcurrentActivityFunctions": 20,
"maxConcurrentOrchestratorFunctions": 20,
"extendedSessionsEnabled": false,
"extendedSessionIdleTimeoutInSeconds": 30,
"useGracefulShutdown": false
}
},
"logging": {
"logLevel": {
"Host.Triggers.DurableTask": "Information"
}
}
}