In MVC Core 2.0 it is recommended (asp-net-core-2-seed-database) that db seeding and other initialization, which is not supposed to run when starting from EF tooling, is placed in Program.cs Main method between call to BuildWebHost and Run.
In order to start up a queue processor singleton my Main looks like this:
public static void Main(string[] args)
{
var host = BuildWebHost(args);
DocumentCreateQueueProcessor qProcessor;
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var config = services.GetRequiredService<IConfiguration>();
qProcessor = new DocumentCreateQueueProcessor(config.GetConnectionString("DefaultConnection"));
}
try
{
host.Run();
}
finally
{
qProcessor = null;
}
}
My queue processor implements IDisposable and uses a cancellationtoken to cancel running threads. At first I just added the nulling after host.Run() but I found out that when setting a breakpoint on statement that it was never reached when debugging the project with IISExpress by pressing F5 in VS. I added the try/finally to the Run statement but my breakpoint is still not reached.
What is the correct way of going about this? Is my code really not reached or is it just the debugger unloading when I close the IISExpress browsing session.