A Simple .NET Core based Message Queue which uses MongoDb.
public class QueueTestMessage
{
public string Message { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}
public class QueueTestMessageConsumer : MongoDbMessageConsumer<QueueTestMessage>
{
protected override Task ConsumeAsync(QueueTestMessage message)
{
Console.WriteLine($"Received message: {message.Message}");
return Task.CompletedTask;
}
}
// Assuming IServiceCollection is already created
services.ConfigureMongoDbMessageQueue(opts =>
{
// Replace with your real connection string
opts.ConnectionString = "mongodb://localhost:27017";
// Replace with your real messaged database name
opts.QueueDbName = "test-messages";
// Register consumers - This is required, only if the application consumes messages
opts.RegisterConsumer<QueueTestMessage, QueueTestMessageConsumer>();
});
// Inject in service
using ScientificBit.MongoDbMQ.Abstraction;
public class MessagePublisher
{
private readonly IMongoDbMessageBus _messageBus;
public class MessagePublisher(IMongoDbMessageBus messageBus)
{
_messageBus = messageBus;
}
public async Task<string> PublishMessage(string msg)
{
var messageId = await _messageBus.PublishAsync(msg);
return messageId;
}
}
TBD
TBD
TBD
Please visit https://www.scientificbit.com