Skip to main content

Definition

Assembly: CloudNimble.SimpleMessageBus.Core.dll Namespace: CloudNimble.SimpleMessageBus.Core

Syntax

CloudNimble.SimpleMessageBus.Core.IMessage

Summary

Defines the required composition of every Message published to the SimpleMessageBus.

Remarks

All messages in the SimpleMessageBus system must implement this interface. The Id property ensures that each message can be uniquely identified throughout its lifecycle, including during processing, error handling, and when moved to poison queues.

Examples

public class OrderCreatedMessage : IMessage
{
    public Guid Id { get; set; } = Guid.NewGuid();
    public string OrderNumber { get; set; }
    public decimal TotalAmount { get; set; }
    public DateTime CreatedAt { get; set; }
}

Properties

Id Abstract

Gets or sets the unique identifier for this Message.

Syntax

System.Guid Id { get; set; }

Property Value

Type: System.Guid A Guid that uniquely identifies this message instance. This value should be set when the message is created and remain constant throughout the message’s lifetime.

Remarks

This identifier is used for tracking messages through the system, deduplication, and correlating messages in poison queues with their original instances.

Methods

CreateChild Extension

Extension method from CloudNimble.SimpleMessageBus.Core.MessageExtensions
Creates a child message that inherits metadata and correlation from the parent.

Syntax

public static TChild CreateChild<TChild>(CloudNimble.SimpleMessageBus.Core.IMessage parent) where TChild : CloudNimble.SimpleMessageBus.Core.MessageBase, new()

Parameters

NameTypeDescription
parentCloudNimble.SimpleMessageBus.Core.IMessageThe parent message.

Returns

Type: TChild A new child message with inherited metadata and correlation.

Type Parameters

  • TChild - The type of child message to create.

Examples

// Create a child event that inherits filtered parent metadata
var shipmentRequested = message.CreateChild&lt;ShipmentRequested&gt;();
shipmentRequested.OrderId = message.OrderId;
shipmentRequested.Items = result.Items;

await publisher.PublishAsync(shipmentRequested);

LastRunSucceeded Extension

Extension method from CloudNimble.SimpleMessageBus.Core.MessageExtensions
Checks if the current handler has already successfully processed this message. This enables idempotent message processing.

Syntax

public static bool LastRunSucceeded(CloudNimble.SimpleMessageBus.Core.IMessage message, string handlerTypeName = null)

Parameters

NameTypeDescription
messageCloudNimble.SimpleMessageBus.Core.IMessageThe message to check.
handlerTypeNamestringThe handler type name. If not provided, attempts to detect from call stack.

Returns

Type: bool true if this handler already ran successfully; otherwise, false.

Examples

public async Task Handle(OrderCreated message, ILogger logger)
{
    if (message.LastRunSucceeded(GetType().Name))
    {
        logger.LogInformation("Order {OrderId} already processed successfully", message.Id);
        return;
    }

    // Process the message...
    message.UpdateResult(true, GetType().Name);
}

UpdateResult Extension

Extension method from CloudNimble.SimpleMessageBus.Core.MessageExtensions
Updates the execution status for the current handler.

Syntax

public static void UpdateResult(CloudNimble.SimpleMessageBus.Core.IMessage message, bool status, string handlerTypeName = null)

Parameters

NameTypeDescription
messageCloudNimble.SimpleMessageBus.Core.IMessageThe message to update.
statusboolThe execution status to record.
handlerTypeNamestringThe handler type name. If not provided, attempts to detect from call stack.

Examples

public async Task Handle(OrderCreated message, ILogger logger)
{
    try
    {
        await ProcessOrder(message);
        message.UpdateResult(true, GetType().Name);
    }
    catch (Exception ex)
    {
        logger.LogError(ex, "Failed to process order");
        message.UpdateResult(false, GetType().Name);
        throw;
    }
}