DI in Azure Functions

后端 未结 11 1076
庸人自扰
庸人自扰 2020-12-25 10:43

I have some class libraries that I use in my ASP.NET Web API app that handle all my backend stuff e.g. CRUD operations to multiple databases like Azure SQL Database, Cosmos

11条回答
  •  执念已碎
    2020-12-25 11:22

    There is an open feature request on the GitHub pages for Azure Functions concerning this matter.

    However, the way I'm approaching this is using some kind of 'wrapper' entry point, resolve this using the service locator and and start the function from there.

    This looks a bit like this (simplified)

    var builder = new ContainerBuilder();
    //register my types
    
    var container = builder.Build();
    
    using(var scope = container.BeginLifetimeScope())
    {
      var functionLogic = scope.Resolve();
    
      functionLogic.Execute();
    }
    

    This is a bit hacky of course, but it's the best there is until there is at the moment (to my knowledge).

提交回复
热议问题