DI in Azure Functions

后端 未结 11 1071
庸人自扰
庸人自扰 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:25

    Azure Functions Depdendency Injection was announced at MSBuild 2019. Here's an example on how to do it:

    [assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
    
    namespace MyNamespace
    {
        public class Startup : FunctionsStartup
        {
            public override void Configure(IFunctionsHostBuilder builder)
            {
                builder.Services.AddHttpClient();
                builder.Services.AddSingleton((s) => {
                    return new CosmosClient(Environment.GetEnvironmentVariable("COSMOSDB_CONNECTIONSTRING"));
                });
                builder.Services.AddSingleton();
            }
        }
    }
    
    • GitHub Example
    • Documentation

提交回复
热议问题