Service Bus Message Pump ExceptionReceived with null Exception
I am looking into the new Service Bus Message Pump programming model and every time I receive a message via a Topic and Subscription the ExceptionReceived event handler is also fired with a null exception. Is this something that is expected and
I should ignore?
This is what my code looks like:
SubscriptionClient client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName);
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = true;
options.MaxConcurrentCalls = 1;
options.ExceptionReceived += options_ExceptionReceived;
client.OnMessage((message) =>
{
...
}, options);
private void options_ExceptionReceived(object sender, ExceptionReceivedEventArgs e)
{
//e.Exception is null here
}
May 30th, 2013 12:41pm
Hi Jeff,
With Azure SDK2.0 came the ability to listen for messages rather than polling for them. I understand you are on same lines,
Here is a sample code snippet (from Abhishek Lal) for a queue client that sit and waits for messages
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.Diagnostics;
using System.Net;
using System.Threading;
namespace SitandWaitForMessageWorkerRole
{
public class WorkerRole : RoleEntryPoint
{
// The name of your queue
const string QueueName = "demoapp";
ManualResetEvent CompletedEvent = new ManualResetEvent(false);
// QueueClient is thread-safe. Recommended that you cache rather than recreating it on every request
QueueClient Client;
public override void Run()
{
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = true; // Indicates if the message-pump should call complete on messages after the callback has completed processing.
options.MaxConcurrentCalls = 1; // Indicates the maximum number of concurrent calls to the callback the pump should initiate
options.ExceptionReceived += ExceptionReceived; // Allows users to get notified of any errors encountered by the message pump
Trace.WriteLine("Starting processing of messages");
// Start receiveing messages
Client.OnMessage((receivedMessage) => // Initiates the message pump and callback is invoked for each message that is recieved, calling close on the client will stop the pump.
{
try
{
// Process the message
Trace.WriteLine("Processing Service Bus message: " + receivedMessage.SequenceNumber.ToString());
}
catch
{
// Handle any message processing specific exceptions here
}
}, options);
CompletedEvent.WaitOne();
}
private void ExceptionReceived(object sender, ExceptionReceivedEventArgs e)
{
if (e.Exception != null)
{
Trace.WriteLine("Error: " + e.Exception.Message);
}
}
public override bool OnStart()
{
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = 5;
// Create the queue if it does not exist already
Trace.WriteLine("Creating Queue");
string connectionString = "*** provide your connection string here***";
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists(QueueName))
{
namespaceManager.CreateQueue(QueueName);
}
// Initialize the connection to Service Bus Queue
Client = QueueClient.CreateFromConnectionString(connectionString, QueueName);
Trace.WriteLine("Sending messages...");
// populate some messages
for (int ctr = 0; ctr < 10; ctr++)
{
Client.Send(new BrokeredMessage());
}
return base.OnStart();
}
public override void OnStop()
{
// Close the connection to Service Bus Queue
Client.Close();
CompletedEvent.Set(); // complete the Run function
base.OnStop();
}
}
---------------------------------- Please mark as answered if it helped Vishal Narayan Saxena http://twitter.com/vishalsaxena http://www.ogleogle.com/vishal/
Free Windows Admin Tool Kit Click here and download it now
May 30th, 2013 2:01pm
I saw that same code sample as well. In that sample he's checking for a non null exception in the event args of the ExceptionReceived handler.
private void ExceptionReceived(object sender, ExceptionReceivedEventArgs e)
{
if (e.Exception != null)
{
Trace.WriteLine("Error: " + e.Exception.Message);
}
}
My question is why would the ExceptionReceived handler ever fire with no exception. In my code it seems to fire every time after I successfully receive a message (via Client.OnMessage). Can I assume there wasn't really a problem if the Exception
property of the ExceptionReceivedEventArgs is null?
May 30th, 2013 2:11pm
It sounds like there are errors happening during message pump or during processing of the message, can you check if the exception is being raised during pump or processing, if it is during pump it is important not to handle the exception in the OnMessage
action else message pump will not know that something went wrong and will simple complete the message, which is something you don't want. you would want that when something goes wrong message is retried a few times, if it doesnt work after a few times it will
be placed in the Dead Letter Queue. But remember, this is only possible if you let the message pump handle the exception for you.
---------------------------------- Please mark as answered if it helped Vishal Narayan Saxena http://twitter.com/vishalsaxena http://www.ogleogle.com/vishal/
Free Windows Admin Tool Kit Click here and download it now
May 30th, 2013 2:33pm
I verified that no exceptions are thrown while I process the message in the OnMessage action.
I also downloaded this code sample from code.msdn.microsoft.com/windowsazure/OnMessage-programing-model-58451157 and ran that against a new service bus namespace and the LogErrors handler is fired for every message received. I posted a question
for that code sample so hopefully Abhishek Lal can answer this.
May 30th, 2013 2:53pm
I got an answer from Abhishek Lal over on his code sample for the OnMessage programming model. He said you can ignore the event if the event args Exception is null.
So checking the exception for null before handling the error is the correct way to handle the event.
private void ExceptionReceived(object sender, ExceptionReceivedEventArgs e)
{
if (e.Exception != null)
{
Trace.WriteLine("Error: " + e.Exception.Message);
}
}
Free Windows Admin Tool Kit Click here and download it now
May 31st, 2013 6:01pm
perfect, but did he tell why the exception is being raised for every message received coz that is absurd---------------------------------- Please mark as answered if it helped Vishal Narayan Saxena http://twitter.com/vishalishere http://www.ogleogle.com/vishal/
June 4th, 2013 12:55pm
perfect, but did he tell why the exception is being raised for every message received coz that is absurd
---------------------------------- Please mark as answered if it helped Vishal Narayan Saxena http://twitter.com/vishalishere http://www.ogleogle.com/vishal/
Nope, just said you can ignore it unless there is an Exception in the event args. I thought the same thing, why raise an ExceptionReceived event when there wasn't really an exception.
Free Windows Admin Tool Kit Click here and download it now
June 5th, 2013 12:01pm