Uses Redis to act as a Message Queue.
dotnet add package Pdsr.Queue.Redis
// Add to DI
builder.Services.AddRedisQueue(new RedisConfiguration
{
EndPoints = new[] { "localhost:6379" }
});
/// <summary>
/// Some message sample
/// </summary>
public class SampleMessage
{
public SampleMessage(string item)
{
Item = item;
}
// can be anything. be careful not to send large objects.
public string Item { get; }
}
app.MapGet("/", async (IRedisQueue redisQueue) =>
{
// create an instance of the message object
string contents = "Peeling an apple";
var message = new SampleMessage(contents);
// publish it
await redisQueue.Publish(message);
// return the message object, just because it's cool to see what we sent to the queue
return Results.Ok(message);//show the message
});
Inject the IRedisQueue
in a worker or some service and fetch messages one at the time
// fetch messages from queue and process them
await _redisQueue.FetchMessage(async (msg) =>
{
Console.WriteLine($"do something with the msg");
return true; // return true to ACK (and discard the message) or return false and do a NACK requeue.
};);
Preferably use a Background worker, as the whole point for me to use Message Queue was to process them separately on a worker not in Services or Controllers.
Make Subscriber pattern to emit events, instead of using a manual fetch
Please refer to contribute. Contributions are welcomed, refer to ToDo
Above is enough for now.