Hi,
I’m trying to create a background service capable of subscribing a message listening to a REDIS channel (would be the same for a classic RABBIT-type message queue).
Steps:
Application.Services.AddService<IConnectionMultiplexer>( x => ConnectionMultiplexer.Connect("192.168.1.178:6379"));
this allows me to inject the object between my instances.
// Injected connectionMultiplexer = Application.Services.GetService<IConnectionMultiplexer>(); // Event from REDIS protected override Task ExecuteAsync(CancellationToken stoppingToken) { var subscriver = connectionMultiplexer.GetSubscriber(); return subscriver.SubscribeAsync("messages", (ch, value) => { // *** Or any other access to a WiseJ object... var toast = new Toast(value); toast.Show(); }); }
I create a background service that takes care of the REDIS channels, in Startup.cs:
using IHost host = Host.CreateDefaultBuilder(args) .ConfigureServices(services => { services.AddHostedService<RedisSubscriver>(); }).Build(); host.RunAsync();
The event works, in fact when sending the message on the queue the debug stops at the code where the WiseJ Toast object is created (***).
It is just not displayed in the main window.
Could you give me a hint or an example to study?
Thanks
Hi Tiziano,
Thank you for your sample, there are a few things that I like to point out regarding how you’re using the background processes.
Any background service (or Hosted Service in this case) has its own separate thread, Wisej.NET’s session doesn’t exist in that thread’s scope, and since it’s not coming from the main application thread, nothing can be updated and/or pushed to the client.
You’ll have to go with another approach and see how it would update your application’s main thread.
You can follow Microsoft’s Documentation in that regard.
But the main point is, what’s happening is that when that breakpoint hits, it’s actually the service thread calling itself and since the Wisej.NET application isn’t scoped, there’s nothing to update!
It is not a Wisej.NET issue, even with a regular ASP.NET Core application this would yield the same result.
If you need additional help, we offer Professional Service packages that you can check out!
HTH,
Alaa
Hi,
As requested, I have created a project containing only one subscriber to redis messages.
docker run -p 6379:6379 --name redis-master -e REDIS_REPLICATION_MODE=master -e ALLOW_EMPTY_PASSWORD=yes bitnami/redis:latest
PUBLISH messages "My Message"
It must display “1” and stop at the break point, in the variable “value” will be the message.
Proceeding however the “Toast” will not display the message
Thank you for your help
Hi Tiziano,
Would you be able to wrap up a small sample for us to see what’s wrong?
We can’t just rely on code snippets, we need to have a small, runnable sample for us to quickly help you out!
Thank you,
Alaa
Please login first to submit.