public DurableSender(MessagingFactory messagingFactory, string serviceBusQueueName) { // Create a Service Bus queue client to send messages to the Service Bus queue. this.queueClient = messagingFactory.CreateQueueClient(serviceBusQueueName); // Create MSMQ queue if it doesn't exit. If it does, open the existing MSMQ queue. this.msmqQueue = MsmqHelper.GetMsmqQueue(MsmqHelper.CreateMsmqQueueName(messagingFactory.Address.DnsSafeHost, serviceBusQueueName, "SEND")); // Create MSMQ deadletter queue if it doesn't exit. If it does, open the existing MSMQ deadletter queue. this.msmqDeadletterQueue = MsmqHelper.GetMsmqQueue(MsmqHelper.CreateMsmqQueueName(messagingFactory.Address.DnsSafeHost, serviceBusQueueName, "SEND_DEADLETTER")); // Start receiving messages from the MSMQ queue. this.MsmqPeekBegin(); }
void MsmqOnPeekComplete(IAsyncResult result) { // Complete the MSMQ peek operation. If a timeout occured, peek again. Message msmqMessage = null; try { msmqMessage = this.msmqQueue.EndPeek(result); } catch (MessageQueueException ex) { if (ex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout) { this.MsmqPeekBegin(); return; } } if (msmqMessage != null) { var brokeredMessage = MsmqHelper.UnpackServiceBusMessageFromMsmqMessage(msmqMessage); // Clone Service Bus message in case we need to deadletter it. var serviceBusDeadletterMessage = brokeredMessage.Clone(); Trace.TraceInformation("DurableSender: Enqueue message {0} into Service Bus.", msmqMessage.Label); switch (this.SendMessageToServiceBus(brokeredMessage)) { case SendResult.Success: // Message was successfully sent to Service Bus. Remove MSMQ message from MSMQ queue. Trace.TraceInformation("DurableSender: Service Bus send operation completed."); this.msmqQueue.BeginReceive(TimeSpan.FromSeconds(60), null, this.MsmqOnReceiveComplete); break; case SendResult.WaitAndRetry: // Service Bus is temporarily unavailable. Wait. Trace.TraceWarning("DurableSender: Service Bus is temporarily unavailable."); this.waitAfterErrorTimer = new Timer( this.ResumeSendingMessagesToServiceBus, null, WaitTimeAfterServiceBusReturnsAnIntermittentErrorInSeconds * 1000, Timeout.Infinite); break; case SendResult.PermanentFailure: // Permanent error. Deadletter MSMQ message. Trace.TraceError("DurableSender: Permanent error when sending message to Service Bus. Deadletter message."); var msmqDeadletterMessage = MsmqHelper.PackServiceBusMessageIntoMsmqMessage(serviceBusDeadletterMessage); try { this.SendtoMsmq(this.msmqDeadletterQueue, msmqDeadletterMessage); } catch (Exception ex) { Trace.TraceError( "DurableSender: Failure when sending message {0} to deadletter queue {1}: {2} {3}", msmqDeadletterMessage.Label, this.msmqDeadletterQueue.FormatName, ex.GetType(), ex.Message); } this.msmqQueue.BeginReceive(TimeSpan.FromSeconds(60), null, this.MsmqOnReceiveComplete); break; } } }
public void Send(BrokeredMessage brokeredMessage) { var msmqMessage = MsmqHelper.PackServiceBusMessageIntoMsmqMessage(brokeredMessage); this.SendtoMsmq(this.msmqQueue, msmqMessage); }