如何在Azure Function App V2启动中调用一些代码逻辑

时间:2019-08-08 18:19:39

标签: c# dependency-injection azure-functions .net-core-2.2 azure-function-app

我正在使用服务总线触发器构建功能应​​用程序v2.0。我已经实现了启动类,但是想知道一种更好的方法,该方法是在类(已注入依赖项)中的方法中解决所有依赖项后在启动时运行的方法。

public class Startup : FunctionsStartup {

    public override void Configure(IFunctionsHostBuilder builder) {
        builder.Services.AddHttpClient();
        builder.Services.AddAutoMapper(typeof(Startup));
        //Need to call Ensurer.EnsureSomething here

    }
}


public class Ensurer {
    private ILogger _logger;
    private IHttpClientFactory _clientFactory;
    public Ensurer(ILogger logger, IHttpClientFactory httpClientFactory) {
        _logger = logger;
        _clientFactory = _clientFactory;
    }

    public void EnsureSomething() {
        var httpClient = _clientFactory.CreateClient();
        //call rest api to ensure prerequisite
    }
}

0 个答案:

没有答案