Working with Envelopes


The IMessageContext.SendEnvelope(Envelope) method allows you to override how a Jasper message is sent and even processed by directly altering the Envelope object that is created for you behind the scenes by IMessageContext.Send() or IMessageContext.Publish(). Here are some sample customizations of Envelope to control how a message is published:


public Task CustomizingEnvelope(IMessageContext bus)
{
    var envelope = new Envelope(new SomeMessage())
    {
        // Override the message routing by a Uri
        Destination = new Uri("tcp://server1:2000"),

        // Make this a scheduled message
        ExecutionTime = DateTime.UtcNow.AddDays(1),

        // Direct the receiver that the sender is interested in any response
        // with this message type

    };

    // This envelope should be discarded if not processed
    // successfully within 5 days
    envelope.DeliverBy = DateTimeOffset.UtcNow.AddDays(5);


    // Discard the message after 20 seconds if
    // not successfully processed before then
    envelope.DeliverWithin(20.Seconds());

    // Send the envelope and its contained message
    return bus.SendEnvelope(envelope);