// Handles both normal and large message send operations private void SendGeneric(bool useLargeMessageSample) { progressBarReceive.Visibility = Visibility.Hidden; Utilities.StartProgressBar(progressBarSend); try { MessageQueueTransaction transaction = new MessageQueueTransaction(); transaction.Begin(); IMessageFormatter formatter = new BinaryMessageFormatter(); Message message = new Message(); FileInfo fileInfo = new FileInfo(textFileName.Text); FileStream fStream = fileInfo.OpenRead(); byte[] b = new byte[fStream.Length]; fStream.Read(b, 0, b.Length); MemoryStream mStream = new MemoryStream(b, 0, b.Length, false); message.Label = fileInfo.Name; message.Formatter = formatter; message.UseDeadLetterQueue = true; message.Body = mStream; int fragmentSize = 0; if (textFragmentSize.Text.Length != 0) { fragmentSize = Int32.Parse(textFragmentSize.Text, CultureInfo.InvariantCulture); } if (useLargeMessageSample) { LargeMessageQueue largeMessageQueue = new LargeMessageQueue(this.queue, fragmentSize); largeMessageQueue.Send(message, transaction); } else { this.queue.Send(message, transaction); } transaction.Commit(); Utilities.StopProgressBar(progressBarSend); this.SetQueueViewerStatus(); MessageBox.Show("Send successful!", "Success!"); } catch (Exception ex) { Utilities.ErrorProgressBar(progressBarSend); this.SetQueueViewerStatus(); MessageBox.Show("Send Error: " + ex.Message, "Error!"); } }
// Handles both lossy and non-lossy large message receive operations private void ReceiveGeneric(bool isLossy) { progressBarSend.Visibility = Visibility.Hidden; Utilities.StartProgressBar(progressBarReceive); try { MessageQueueTransaction transaction = new MessageQueueTransaction(); transaction.Begin(); if (isLossy) { this.queue.Receive(new TimeSpan(0, 0, 10)); } LargeMessageQueue largeMessageQueue = new LargeMessageQueue(this.queue, 0); System.Messaging.Message message = largeMessageQueue.Receive(new TimeSpan(0, 0, 10), transaction); // Redundant. Will never execute these for lossy receive message.Formatter = new BinaryMessageFormatter(); MemoryStream mStream = (MemoryStream)message.Body; string newLabel = "Received - " + message.Label; FileStream fStream = new FileStream(newLabel, FileMode.Create, FileAccess.ReadWrite); mStream.WriteTo(fStream); mStream.Close(); fStream.Close(); transaction.Commit(); this.LoadImageFile(imageReceiveFile, newLabel); imageReceiveFile.Visibility = Visibility.Visible; Utilities.StopProgressBar(progressBarReceive); this.SetQueueViewerStatus(); MessageBox.Show("Receive successful!", "Success!"); } catch (LargeMessageQueueException lmqe) { Utilities.ErrorProgressBar(progressBarReceive); imageReceiveFile.Visibility = Visibility.Hidden; this.SetQueueViewerStatus(); MessageBox.Show("Receive Error: " + lmqe.Message + "\n Large Sequence Id:" + lmqe.CorrelationId, "Error!"); } catch (Exception ex) { Utilities.ErrorProgressBar(progressBarReceive); imageReceiveFile.Visibility = Visibility.Hidden; this.SetQueueViewerStatus(); MessageBox.Show("Receive Error: " + ex.Message, "Error!"); } }