roryk:
In aspnet core (or .net5), if I want a server to only process specific queues then within ConfigureServices()
I should do something like this:
string[] queues = new[] { "one", "two" };
services.AddHangfireServer(options => { options.Queues = queues; });
But if I need to use some other .net objects to get my array of queue names (e.g. in my case, connect to a database to look them up) it’s quite hard to do this within ConfigureServices() since no serviceProvider is available and you shouldn’t build one as it’ll create an additional copy of singletons. Does anyone have a good pattern for solving this? The answers here don’t appear straightforward since the AddHangfireServer()
method isn’t a trivial AddSingleton()
call.
Currently I’m thinking it’ll be easiest to directly instantiate the objects I need within ConfigureServices, before using them to call AddHangfireServer(), but that’s not super simple in my case so I’d love to hear how anyone else has solved this.
thanks for any tips.
Rory
Hi guys,
I use this code to implement this:
private List<string> GetQueuesHangFire(IApplicationBuilder app)
{
using var serviceScope = app.ApplicationServices
.GetRequiredService<IServiceScopeFactory>()
.CreateScope();
using var context = serviceScope.ServiceProvider.GetService<Models.DB.MyCustomContext>();
var res = context.Queues.Select(x => x.Code).ToList();
res.Add("default");
return res;
}
I hope that this help you.
Regards
Gon