【发布时间】:2020-05-12 13:28:54
【问题描述】:
我想使用 Simple Injector 在 Azure Fuctions 中注入命令处理程序、ILogger 和 TelemetryClient。
这是我的 Azure 函数:
[FunctionName("ReceiveEvent")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log,
ICommandMapper commandMapper,
ICommandValidator commandValidator,
ICommandHandlerService commandHandlerService)
{
log.LogInformation("ReceiveEvent HTTP trigger function started processing request.");
IActionResult actionResult = null;
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var command = await commandMapper.Map(requestBody);
if (commandValidator.Validate(req, command, ref actionResult))
{
//TODO
commandHandlerService.HandleCommand(command);
return actionResult;
}
return actionResult;
}
这是我的引导程序类:
public class Bootstrapper
{
public static void Bootstrap(IEnumerable<Assembly> assemblies = null, bool verifyContainer = true)
{
Container container = new Container();
container.Register(typeof(ICosmosDBRepository<>), typeof(CosmosDBRepository<>).Assembly);
container.Register(typeof(IAzureBlobStorage), typeof(AzureBlobStorage).Assembly);
container.Register(typeof(ICommandMapper), typeof(CommandMapper).Assembly);
container.Register(typeof(ICommandValidator), typeof(CommandValidator).Assembly);
container.Register(typeof(ICommandHandlerService), typeof(CommandHandlerService).Assembly);
List<Assembly> myContextAssemlies = new List<Assembly>
{
Assembly.GetAssembly(typeof(CardBlockCommandHandler)),
};
container.Register(typeof(ICommandHandler), myContextAssemlies, Lifestyle.Scoped);
assemblies = assemblies == null
? myContextAssemlies
: assemblies.Union(myContextAssemlies);
if (verifyContainer)
{
container.Verify();
}
}
}
现在我的问题是,我将如何在 Azure Function 中使用此引导程序方法解决 DI?
我需要在 FunctionsStartup 中注册引导方法吗?
【问题讨论】:
-
@Steven 我更新了我的问题。你能建议吗
-
@Steven 我已经更新了我的问题,请您现在提出建议
-
@Steven 我已经更新了我的天蓝色函数,
标签: c# dependency-injection azure-functions simple-injector