Hi MS,
I was hoping to run a custom IHostedService before the main Functions Host starts.
Is this possible?
For example -> I have a few queue triggers. They work 100% great. I was hoping to run my custom IHostedService which creates those queues if they don't exist already. Once that custom host finishes, then the main/normal Host will start/run.
I thought this might be the way, but wasn't sure if this is the recommendation:
program.cs
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices((appBuilder, services) =>
{
// Create an initialization service, which SHOULD run before the main function service.
services.AddSingleton<IHostedService, InitializationService>();
}
.Build();
host.Run();
InitializationService.cs
public class InitializationService : IHostedService
{
private readonly string _azureStorageConnectionString;
private readonly ILogger<InitializationService> _logger;
public InitializationService(IOptions<ConnectionStringOptions> connectionStringOptions, ILogger<InitializationService> logger)
{
_azureStorageConnectionString = connectionStringOptions.Value.AzureStorage;
_logger = logger;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
// My logic in here, like creating an azure storage QueueClient and then creating the queue(s) if they don't exist.
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
Can anyone help?