For the complete documentation index, see llms.txt. This page is also available as Markdown.

MongoDb Inbox

Reference · Applies to Brighter V10

MongoDB Inbox Usage

The MongoDB Inbox allows use of MongoDB for Brighter's inbox support. The configuration is described in Dispatcher Configuration Reference.

For this we will need the Inbox packages for the MongoDB Inbox.

  • Paramore.Brighter.Inbox.MongoDb

  • Paramore.Brighter.MongoDb

MongoDB Inbox Configuration

Basic Configuration

private static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureServices((hostContext, services) =>
        {
            ConfigureBrighter(hostContext, services);
        });

private static void ConfigureBrighter(HostBuilderContext hostContext, IServiceCollection services)
{
    // MongoDB connection string
    var connectionString = "mongodb://localhost:27017";
    var databaseName = "BrighterDatabase";
    
    // Configure MongoDB
    var mongoDbConfiguration = new MongoDbConfiguration(connectionString, databaseName)
    {
        Inbox = new MongoDbCollectionConfiguration
        {
            Name = "Inbox",
            MakeCollection = OnResolvingACollection.Validate,
            TimeToLive = TimeSpan.FromDays(7) // Optional: Auto-expire messages after 7 days
        }
    };

    services.AddConsumers(options =>
    {
        options.InboxConfiguration = new InboxConfiguration(
            new MongoDbInbox(mongoDbConfiguration),
            scope: InboxScope.Commands,
            onceOnly: true,
            actionOnExists: OnceOnlyAction.Throw
        );
    });
}

In Brighter 10.7.0 this configuration takes effect only in an application that also calls AddProducers; see Global Inbox Configuration in a Consumer-Only Application.

Advanced Configuration

For more advanced scenarios, you can provide custom MongoDB client settings and collection configurations:

Using a Connection Provider

You can also use a custom connection provider for more control over the MongoDB connection:

MongoDB Inbox Collection Management

The MongoDB Inbox supports different strategies for collection management through the MakeCollection property:

  • OnResolvingACollection.Assume: Assumes the collection already exists (default behavior)

  • OnResolvingACollection.Validate: Validates that the collection exists, throws an exception if it doesn't

  • OnResolvingACollection.CreateIfNotExists: Creates the collection if it doesn't exist

MongoDB Inbox Time-To-Live (TTL) Support

The MongoDB Inbox supports automatic message expiration using MongoDB's TTL feature. You can configure this through the TimeToLive property in the collection configuration:

When TTL is configured, MongoDB will automatically create a TTL index on the TimeStamp field and remove expired documents.

MongoDB Inbox Message Structure

The MongoDB Inbox stores messages with the following structure:

  • Id: Composite key containing the command ID and context key

  • TimeStamp: When the message was created

  • CommandType: The full type name of the command

  • CommandBody: JSON serialized command data

  • ExpireAfterSeconds: Optional expiration time for individual messages

MongoDB Inbox Error Handling

The MongoDB Inbox will throw appropriate exceptions for various error conditions:

  • ArgumentException: When required configuration is missing

  • InboxException: For inbox-specific errors like duplicate messages when OnceOnlyAction.Throw is configured

Dependencies

The MongoDB Inbox requires the following NuGet packages:

Last updated

Was this helpful?