In the startup.cs class, under ConfigureServices method add the below extension method as below.
using PivotalServices.Redis.Messaging;publicvirtualvoidConfigureServices(IServiceCollectionservices){
services.AddRedisMessagingConsumer();}
Next step is to subscribe to a channel to receive messages.
PivotalServices.Redis.Messaging.IConsumer implementation will be in the dependency container now.
Here, I would use the Configure method of startup.cs to demonstrate it (as below). But you can always inject PivotalServices.Redis.Messaging.IConsumer into any of the classes and perform the necessary operations there.
using PivotalServices.Redis.Messaging;publicvoidConfigure(IApplicationBuilderapp,IConsumerconsumer){
consumer.StartConsumption("myChannel",(message)=>{//Any action to be performed when a message is received Console.Out.WriteLine($"Received Message, {message.Id}");});}
Similarly, to unsubscribe from a channel you can call StopConsumption as below, using IApplicationLifetime
using PivotalServices.Redis.Messaging;publicvoidConfigure(IApplicationBuilderapp,IApplicationLifetimelifetime,IConsumerconsumer){
lifetime.ApplicationStopping.Register(()=> consumer.StopConsumption("myChannel"));}
In the startup.cs class, under ConfigureServices method add the below extension method as below.
using PivotalServices.Redis.Messaging;publicvirtualvoidConfigureServices(IServiceCollectionservices){
services.AddRedisMessagingProducer();}
Next step is to subscribe to a channel to receive messages.
PivotalServices.Redis.Messaging.IProducer implementation will be in the dependency container
I would use the Configure method of startup.cs to demonstrate it (as below - publish a message every 10 seconds). But you can always inject PivotalServices.Redis.Messaging.IProducer into any of the classes and publish messages there.
using PivotalServices.Redis.Messaging;publicvoidConfigure(IApplicationBuilderapp,IProducerproducer){while(true){
producer.Publish("myChannel",new Message(Guid.NewGuid().ToString(), DateTime.Now.ToString()));
Thread.Sleep(10000);}}
Note: The channel name includes pattern matching, if it contains a *