File Share DataBus Usage

Component: File Share Databus
NuGet Package NServiceBus (6.x)
  1. Run the solution. Two console applications start.
  2. Find the Sender application by looking for the one with "Sender" in its path
  3. Press d in the window to send a large message. A message has just been sent that is larger than the limit allowed by the learning transport. NServiceBus sends it as an attachment, allowing it to reach the Receiver application.
  4. Click 'n' in the Sender window. A message larger than the allowed limit is sent, but this time without utilizing the NServiceBus attachments mechanism. An exception is thrown in the "Sender" application.
The FileShareDataBus does not remove physical attachments once the message has been processed. Apply a custom cleanup-strategy.

Code walk-through

This sample contains three projects:

  • Messages - A class library containing the sample messages. Only one of the message types utilizes the DataBus.
  • Sender - A console application responsible for sending the large messages.
  • Receiver - A console application responsible for receiving the large messages from Sender.

Messages project

Look at the two messages in the Messages project. Start with the large message that is not utilizing the DataBus mechanism. The message is a simple byte array command:

public class AnotherMessageWithLargePayload :
    public byte[] LargeBlob { get; set; }

The other message utilizes the DataBus mechanism:

//the data bus is allowed to clean up transmitted properties older than the TTBR
public class MessageWithLargePayload :
    public string SomeProperty { get; set; }
    public DataBusProperty<byte[]> LargeBlob { get; set; }

DataBusProperty<byte[]> instructs NServiceBus to treat the LargeBlob property as an attachment. It is not transported in the normal serialization flow.

When sending a message using the FileShare DataBus, the message's payload resides in the directory. In addition, a 'signaling' message is sent to the Receiving endpoint.

The TimeToBeReceived attribute instructs the NServiceBus framework that it is allowed to clean the message after one minute if it was not received by the receiver. The message payload remains in the storage directory after the message is cleaned by the NServiceBus framework.

Following is an example of the signaling message that is sent to the receiving endpoint:

	"SomeProperty":"This message contains a large blob that will be sent on the data bus",

Configuring the databus location

Both the Sender and Receive project must share a common location to store large binary objects. This is done by calling FileShareDataBus. This code instructs NServiceBus to use the FileSharing transport mechanism for the attachment.

var dataBus = endpointConfiguration.UseDataBus<FileShareDataBus>();

Sender project

The following Sender project code sends the MessageWithLargePayloadmessage, using the NServiceBus attachment mechanism:

var message = new MessageWithLargePayload
    SomeProperty = "This message contains a large blob that will be sent on the data bus",
    LargeBlob = new DataBusProperty<byte[]>(new byte[1024*1024*5]) //5MB
await endpointInstance.Send("Samples.DataBus.Receiver", message)

The following Sender project code sends the AnotherMessageWithLargePayload message without using the NServiceBus attachment mechanism:

var message = new AnotherMessageWithLargePayload
    LargeBlob = new byte[1024*1024*5] //5MB
await endpointInstance.Send("Samples.DataBus.Receiver", message)

In both cases, a 5MB message is sent, but the MessageWithLargePayload message is successfully delivered, while AnotherMessageWithLargePayload fails.

Receiver project

This is the receiving message handler:

public class MessageWithLargePayloadHandler :
    static ILog log = LogManager.GetLogger<MessageWithLargePayloadHandler>();

    public Task Handle(MessageWithLargePayload message, IMessageHandlerContext context)
        log.Info($"Message received, size of blob property: {message.LargeBlob.Value.Length} Bytes");
        return Task.CompletedTask;

Related Articles

  • DataBus
    How to handle messages that are too large to be sent by a transport.

Last modified