Contents
|
NServiceBus Attachments SampleLarge chunks of data, such as images or video files can be transported using NServiceBus v3 data bus. This is particularly important when running in cloud environments where limits on message size is usually much lower than on-premise. In order to see how to send and receive attachments in NServiceBus, open the Databus sample.
First of all run the solution.
Then, press the 'e' and 'Enter'. Larger than the allowed 4 Mb message is sent, but this time, without utilizing NServiceBus attachments mechanism. An exception is thrown at the "Sender" application. You consoles view should like the following:
let's go look at the code:
Code Walk-ThroughThis sample contains three projects:
Receiver.Messages projectLets have a look at the Receiver.Messages project, at the two defined messages. Lets start with the large one, that is not utilizing Databus mechanism: publicc class AnotherMessageWithLargePayload : IMessage { public byte[]LargeBlob { get; set; } } The above message is a simple byte array message. Lets examine the other message, that is utilizing Databus mechanism:
[TimeToBeReceived("00:01:00")] public class MessageWithLargePayload:IMessage { public string SomeProperty { get; set; } public DataBusProperty<byte[]> LargeBlob { get; set; } } The thing to pay attention to is the following type
DataBusProperty<byte[]>
This is an NServiceBus data type that instruct NServiceBus to treat the LargeBlob property as an attachment.
Let's understand the following attribute:
[TimeToBeReceived("00:01:00")]
When sending a message using the NServiceBus Message attachments mechanism, the
message's payload resides in the folder, in addition, a 'signaling'
message is sent to the Receiving endpoint. The following is an example of the 'signaling' message that will be sent to the receiving endpoint: <?xml version="1.0"?> <Messages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.net/Receiver.Messages"> <MessageWithLargePayload> <SomeProperty>This message contains a large blob that will be sent on the data bus</ SomeProperty> <LargeBlob> <Key>2012-01-02_10\f83e3641-4588-4cb2-8c4f-4077342ed32e</Key> <HasValue>true</HasValue> </LargeBlob> </MessageWithLargePayload> </Messages> SenderLets look at the Sender project, to see how to configure the NServiceBus to use to handle attachments. Lets start with Sender project app.config: <MessageEndpointMappings> <add Messages="Receiver.Messages" Endpoint="Receiver" /> </MessageEndpointMappings> The sender instructing NServiceBus to send messages with Namespace equal to Receiver.Messages to the Receiver endpoint Lets open the EndpointConfig in the Sender project: public static string BasePath = "..\\..\\..\\storage"; public void Init() { Configure.Instance .FileShareDataBus(BasePath) .UnicastBus().DoNotAutoSubscribe();//until ICommand is introduced } This code, instructs NServiceBus to use FileSharing transport mechanism for the
attachment.
The DoNotAutoSubscribe tells NServiceBus not to use a publish/subscribe mechanism. If we were using ICommand for our large message, no automatically subscription was to occur. The following Sender project code, send the MessageWithLargePayload message utilizing NServiceBus attachment mechanism:
bus.Send<MessageWithLargePayload>(m =>
{
m.SomeProperty = "This message contains a large blob that will be sent on the data bus";
m.LargeBlob = new DataBusProperty<byte[]>(new byte[1024 * 1024 * 5]);//5MB
});
The following Sender project code, send the AnotherMessageWithLargePayload message without utilizing NServiceBus attachment mechanism:
bus.Send<AnotherMessageWithLargePayload>(m =>
{
m.LargeBlob = new byte[1024 * 1024 * 5];//5MB
});
In both cases, a 5Mb message is being sent, but in the MessageWithLargePayload will go through while the AnotherMessageWithLargePayload will fail. Lets see to the Receiver project to see that the receiving application: Receiver ProjectThe endpoint configuration code of Receiver is identical to this of the Sender: Lets open the EndpointConfig in the Sender project: public static string BasePath = "..\\..\\..\\storage"; public void Init() { Configure.Instance .FileShareDataBus(BasePath) .UnicastBus().DoNotAutoSubscribe(); } The following is the receiving message handler: public class MessageWithLargePayloadHandler : IHandleMessages<MessageWithLargePayload> { public void Handle(MessageWithLargePayload message) { Console.WriteLine("Message received, size of blob property: " + message.LargeBlob.Value.Length + " Bytes"); } } Where to go next?If you are not familiar with Unobtrusive messaging mode, read the documentation here or see the working sample here. |