我想使用Simple Injector在Azure函数中注入命令处理程序、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中注册引导方法吗?
使用Simple Injector,您不会将服务注入Azure函数。不支持此操作。相反,集成指南解释道:
与其将服务注入Azure功能,不如将所有业务逻辑从Azure功能移到应用程序组件中。函数的依赖项可以成为组件构造函数的参数。此组件可以从Azure函数中解析和调用。
对于您的特定情况,这意味着执行以下步骤:
1.将所有业务逻辑从Azure功能移到应用程序组件中。函数的依赖项可以成为组件构造函数的参数
public sealed class ReceiveEventFunction
{
private readonly ILogger log;
private readonly ICommandMapper commandMapper;
private readonly ICommandValidator commandValidator;
private readonly ICommandHandlerService commandHandlerService;
public ReceiveEventFunction(ILogger log, ICommandMapper commandMapper,
ICommandValidator commandValidator, ICommandHandlerService commandHandlerService)
{
this.log = log;
this.commandMapper = commandMapper;
this.commandValidator = commandValidator;
this.commandHandlerService = commandHandlerService;
}
public async Task<IActionResult> Run(HttpRequest req)
{
IActionResult actionResult = null;
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var command = await commandMapper.Map(requestBody);
if (commandValidator.Validate(req, command, ref actionResult))
{
commandHandlerService.HandleCommand(command);
return actionResult;
}
return actionResult;
}
}
2此组件可以从Azure函数中解析和调用
[FunctionName("ReceiveEvent")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
{
// Resolve the service
var service = Bootstrapper.Container.GetInstance<ReceiveEventFunction>();
// Invoke the service
service.Run(req);
}
3.最后步骤
要完成配置,您必须确保(新的(静态Bootstrapper.Container
字段存在,并且ReceiveEventFunction
已注册:
public class Bootstrapper
{
public static readonly Container Container;
static Bootstrapper()
{
Container = new Bootstrapper().Bootstrap();
}
public static void Bootstrap(
IEnumerable<Assembly> assemblies = null, bool verifyContainer = true)
{
Container container = new Container();
container.Register<ReceiveEventFunction>();
... // rest of your configuration here.
}
}