Deprecated TransportMessage in Version 6

Component: NServiceBus

Previous versions used TransportMessage as a generic container both for outgoing and incoming messages. For a better separation of concerns that class has been split into IncomingMessage and OutgoingMessage. So all code paths related to outgoing messages will use OutgoingMessage and all code paths related to incoming messages will use IncomingMessages. The class TransportMessage has been deprecated entirely. Here are a few common scenarios related to TransportMessage and how they can be addressed with either IncomingMessage or OutgoingMessage.

Body

Both IncomingMessage and OutgoingMessage provide a body byte array to get access to the underlying payload of the property Body.

When setting the body, raw sending is the most likely scenario. See section Raw sending.

Headers

Both IncomingMessage and OutgoingMessage provide a headers dictionary to get or set headers of the property Headers.

Id

Both IncomingMessage and OutgoingMessage provide a message ID of the property MessageId.

CorrelationId

The correlation ID is no longer a strongly typed property exposed. To get access to the correlation ID of a message use the Headers.CorrelationId key.

ReplyAddress

The ReplyAddress can only be accessed on an incoming message. Use the extension method GetReplyAddress on IncomingMessage to acquire the reply address.

MessageIntent

The MessageIntent can only be accessed on an incoming message. Use the extension method GetMessageIntent on IncomingMessage to acquire the message intent.

TimeToBeReceived

From the perspective of an outgoing message the TimeToBeReceived is a delivery concern and needs to be specified over the newly introduced DeliveryConstraint.

Set the TimeToBeReceived

6.x NServiceBus
var timeToBeReceived = TimeSpan.FromSeconds(25);
var deliveryConstraint = new DiscardIfNotReceivedBefore(timeToBeReceived);
context.Extensions.AddDeliveryConstraint(deliveryConstraint);

Read the TimeToBeReceived

6.x NServiceBus
context.Extensions.TryGetDeliveryConstraint(out DiscardIfNotReceivedBefore constraint);
timeToBeReceived = constraint.MaxTime;

From the perspective of an incoming message the TimeToBeReceived can be acquired by using the Headers.TimeToBeReceived on the IncomingMessage.Headers dictionary.

Recoverable

From the perspective of an outgoing message the Recoverable flag is a delivery concern and needs to be specified over the newly introduced DeliveryConstraint.

Set the Recoverable

6.x NServiceBus
context.Extensions.AddDeliveryConstraint(new NonDurableDelivery());

Read the Recoverable

6.x NServiceBus
context.Extensions.TryGetDeliveryConstraint(out NonDurableDelivery constraint);

From the perspective of an incoming message, the Recoverable flag can be acquired by using the Headers.NonDurableMessage on the IncomingMessage.Headers dictionary.

Raw sending

In Version 5 it was possible to use ISendMessages to do raw sends. In Version 6 IDispatchMessages was introduced. See the following snippet how raw sending could look like

6.x NServiceBus
var headers = new Dictionary<string, string>();
var outgoingMessage = new OutgoingMessage("MessageId", headers, new byte[]
{
});
var constraints = new List<DeliveryConstraint>
{
    new NonDurableDelivery()
};
var address = new UnicastAddressTag("Destination");
var operation = new TransportOperation(
    message: outgoingMessage,
    addressTag: address,
    requiredDispatchConsistency: DispatchConsistency.Default,
    deliveryConstraints: constraints);
var operations = new TransportOperations(operation);
await dispatcher.Dispatch(operations, new TransportTransaction(), new ContextBag())
    .ConfigureAwait(false);

Last modified