Where is the IoC happening in an Azure Durable Function?

Viewed 299

This code comes directly from the Durable Function startup in Visual Studio 2019

    [FunctionName("Orchestrator_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 instanceId = await starter.StartNewAsync("Orchestrator", null);

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

        return starter.CreateCheckStatusResponse(req, instanceId);
    }

Where are the values for IDurableOrchestationClient starter & ILogger log coming from? Since these parameters wont be passed in the HTTP request, I'm assuming that there must be some IoC magic happening behind the scenes, but I'm not entirely sure what/where it is.

1 Answers

I'm assuming that there must be some IoC magic happening behind the scenes.

Correct.

Where is the value for IDurableOrchestrationClient coming from?

The IoC is from AddDurableTask (source). IDurableClientFactory creates IDurableClient which inherits from IDurableOrchestrationClient. You can find an example on usage of AddDurableTask here.

serviceCollection.TryAddSingleton<IDurableClientFactory, DurableClientFactory>();

Where is the value for ILogger coming from?

(as @Ian Kemp and @Nkosi already pointed out)

The IoC is from AddWebScriptHost (source). ILoggerFactory creates ILogger.

loggingBuilder.Services.AddSingleton<ILoggerFactory, ScriptLoggerFactory>();
Related