IoC 在 Azure Durable Function 中发生在哪里?
Where is the IoC happening in an Azure Durable Function?
此代码直接来自 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);
}
IDurableOrchestationClient starter
和 ILogger log
的值来自哪里?由于这些参数不会在 HTTP 请求中传递,我假设一定有一些 IoC 魔术在幕后发生,但我不完全确定 what/where 它是。
I'm assuming that there must be some IoC magic happening behind the scenes.
正确。
Where is the value for IDurableOrchestrationClient
coming from?
IoC 来自 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?
(正如@Ian Kemp 和@Nkosi 已经指出的那样)
IoC 来自 AddWebScriptHost
(source)。 ILoggerFactory
创建 ILogger
.
loggingBuilder.Services.AddSingleton<ILoggerFactory, ScriptLoggerFactory>();
此代码直接来自 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);
}
IDurableOrchestationClient starter
和 ILogger log
的值来自哪里?由于这些参数不会在 HTTP 请求中传递,我假设一定有一些 IoC 魔术在幕后发生,但我不完全确定 what/where 它是。
I'm assuming that there must be some IoC magic happening behind the scenes.
正确。
Where is the value for
IDurableOrchestrationClient
coming from?
IoC 来自 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?
(正如@Ian Kemp 和@Nkosi 已经指出的那样)
IoC 来自 AddWebScriptHost
(source)。 ILoggerFactory
创建 ILogger
.
loggingBuilder.Services.AddSingleton<ILoggerFactory, ScriptLoggerFactory>();