2

I am building a function app v2.0 with service bus trigger. I have the startup class implemented, but want to know the better way of calling a method which is in a class (with dependencies injected) that should run in startup after all the dependencies are resolved.

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
    }
}
youkarthik
  • 151
  • 3
  • I asked a similar question [here](https://stackoverflow.com/questions/56419724/initialize-a-singleton-after-configuring-dependency-injection-but-before-the-fi). But it appears there is no easy way to do this because functions DI doesn't expose 'Ensure' kind of semantics. Asp.net core supports this via Configure methods, but not functions :( At present, I call Ensure/init in constructor of my singleton. That is not ideal because I would like ctor to be very light. – Turbo Aug 08 '19 at 18:43
  • It need not be ensure, but at least there should be able to fire and forget – youkarthik Aug 08 '19 at 18:46

0 Answers0