Внедрение зависимостей возможно и в функциях Durable.
Установите и добавьте ссылку на следующий пакет из Nuget:
Microsoft.Azure.Functions.Extensions.DependencyInjection
Вот рабочий пример файла запуска с выполненным внедрением зависимости:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
[assembly: FunctionsStartup(typeof(MyDurableFunction.Startup))]
namespace MyDurableFunction
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
// Get the path to the folder that has appsettings.json and other files.
var currentDirectory = "/home/site/wwwroot";
if (IsDevelopmentEnvironment())
{
currentDirectory = Environment.CurrentDirectory;
}
// uncomment this line for local debugging
// currentDirectory = Environment.CurrentDirectory;
var environment = Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT");
var config = new ConfigurationBuilder()
.SetBasePath(currentDirectory)
// environment specific settings go here
.AddJsonFile($"settings.{environment}.json", optional: false, reloadOnChange: false)
.AddEnvironmentVariables()
.Build();
builder.Services.AddSingleton<IConfiguration>(config);
builder.Services.AddSingleton<IMyBusinessService1>(sp =>
{
var secretFromAppSettings = config["secretKey"];
var passwordFromAppSettings = config["secretPassword"];
return new MyBusinessService1(secretFromAppSettings, passwordFromAppSettings);
});
builder.Services.AddSingleton<IDatabaseHelper, DatabaseHelper>();
builder.Services.AddLogging();
}
public bool IsDevelopmentEnvironment()
{
return "Local".Equals(Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT"), StringComparison.OrdinalIgnoreCase);
}
}
}
Надеюсь, это поможет!