Azure Durable Function 中的 IoC 发生在哪里?

Chr*_*ips 4 c# dependency-injection azure azure-functions azure-durable-functions

此代码直接来自 Visual Studio 2019 中的 Durable Function 启动

    [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);
    }
Run Code Online (Sandbox Code Playgroud)

IDurableOrchestationClient starter&的值ILogger log来自哪里?由于这些参数不会在 HTTP 请求中传递,我假设幕后一定有一些 IoC 魔法发生,但我不完全确定它是什么/在哪里。

Han*_*hao 5

我假设幕后一定有一些 IoC 魔法发生。

正确的。

价值IDurableOrchestrationClient从何而来?

IoC 来自AddDurableTask( source )。IDurableClientFactory创建IDurableClient继承自IDurableOrchestrationClient. 您可以在AddDurableTask 此处找到有关使用的示例。

serviceCollection.TryAddSingleton<IDurableClientFactory, DurableClientFactory>();
Run Code Online (Sandbox Code Playgroud)

价值ILogger从何而来?

(正如@Ian Kemp 和@Nkosi 已经指出的那样)

IoC 来自AddWebScriptHost( source )。ILoggerFactory创建ILogger.

loggingBuilder.Services.AddSingleton<ILoggerFactory, ScriptLoggerFactory>();
Run Code Online (Sandbox Code Playgroud)