diff --git a/src/Orchard.Web/Modules/Orchard.MessageBus/Brokers/SqlServer/Worker.cs b/src/Orchard.Web/Modules/Orchard.MessageBus/Brokers/SqlServer/Worker.cs index dbeee48da..fe8980008 100644 --- a/src/Orchard.Web/Modules/Orchard.MessageBus/Brokers/SqlServer/Worker.cs +++ b/src/Orchard.Web/Modules/Orchard.MessageBus/Brokers/SqlServer/Worker.cs @@ -30,7 +30,7 @@ namespace Orchard.MessageBus.Brokers.SqlServer { private static int lastMessageId = 0; private bool _stopped; - private Dictionary>> _handlers = new Dictionary>>(); + private Dictionary>> _handlers = new Dictionary>>(); public Worker(ShellSettings shellSettings, IHostNameProvider hostNameProvider) { _hostNameProvider = hostNameProvider; @@ -60,7 +60,7 @@ namespace Orchard.MessageBus.Brokers.SqlServer { // load and process existing messages using (var connection = new SqlConnection(_shellSettings.DataConnectionString)) { connection.Open(); - + var command = CreateCommand(connection); messages = GetMessages(command); } @@ -86,7 +86,7 @@ namespace Orchard.MessageBus.Brokers.SqlServer { connection.Open(); using (var command = CreateCommand(connection)) { - + // create a sql depdendency on the table we are monitoring _dependency = new SqlDependency(command); @@ -101,6 +101,10 @@ namespace Orchard.MessageBus.Brokers.SqlServer { private void ProcessMessages(IEnumerable messages) { + if (!messages.Any()) { + return; + } + // if this is the first time it's executed we just need to get the highest Id if (lastMessageId == 0) { lastMessageId = messages.Max(x => x.Id); @@ -109,7 +113,7 @@ namespace Orchard.MessageBus.Brokers.SqlServer { // process the messages synchronously and in order of publication foreach (var message in messages.OrderBy(x => x.Id)) { - + // save the latest message id so that next time the table is monitored // we get notified for new messages lastMessageId = message.Id; @@ -145,17 +149,17 @@ namespace Orchard.MessageBus.Brokers.SqlServer { GetHandlersForChannel(channel).Add(handler); } - private List> GetHandlersForChannel(string channel) { - List> channelHandlers; - - if(!_handlers.TryGetValue(channel, out channelHandlers)) { - channelHandlers = new List>(); - _handlers.Add(channel, channelHandlers); - } + private List> GetHandlersForChannel(string channel) { + List> channelHandlers; - return channelHandlers; + if (!_handlers.TryGetValue(channel, out channelHandlers)) { + channelHandlers = new List>(); + _handlers.Add(channel, channelHandlers); } + return channelHandlers; + } + public SqlCommand CreateCommand(SqlConnection connection) { SqlCommand command = new SqlCommand(commandText, connection); @@ -195,6 +199,6 @@ namespace Orchard.MessageBus.Brokers.SqlServer { return result; } - + } } \ No newline at end of file