I read a post that dealt with the same problem I'm having. It is located at https://social.msdn.microsoft.com/Forums/en-US/069653b5-c233-4942-85b2-9eb50b3865c7/azure-service-bus-subscription-how-do-i-delete-the-message-when-i-receive-a?forum=servbus
I'm calling message.Complete() and I'm getting the MessageLockLostException but the difference with my issue is that I used the code that someone posted to fix the issue and it isn't working for me. I have pasted my full code below to show you how my program
is working. What am I doing wrong and/or how do I fix this issue?
static void Main(string[] args)
{
try
{
string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
QueueClient client = QueueClient.CreateFromConnectionString(connectionString, "OoplesQueue");
var actionBlock = new ActionBlock<BrokeredMessage>(async message =>
await processCalculations(message),
new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount
});
var produceMessagesTask = Task.Run(async () => await
ProduceBrokeredMessagesAsync(client,
actionBlock));
produceMessagesTask.Wait();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
private static async Task ProduceBrokeredMessagesAsync(QueueClient client,
ActionBlock<BrokeredMessage> actionBlock)
{
BrokeredMessage message;
while ((message = await client.ReceiveAsync()) != null)
{
await actionBlock.SendAsync(message);
}
}
public static ConnectionMultiplexer connection;
public static IDatabase cache;
public static async Task processCalculations(BrokeredMessage message)
{
var brokeredMessageRenewCancellationTokenSource = new CancellationTokenSource();
try
{
if (message != null)
{
if (connection == null || !connection.IsConnected)
{
connection = await ConnectionMultiplexer.ConnectAsync("connectionString");
//connection = ConnectionMultiplexer.Connect("ConnectionString,SyncTimeout=10000,ConnectTimeout=10000");
}
cache = connection.GetDatabase();
var brokeredMessageRenew = Task.Run(async () =>
{
while (!brokeredMessageRenewCancellationTokenSource.Token.IsCancellationRequested)
{
//Based on LockedUntilUtc property to determine if the lock expires soon
if (DateTime.UtcNow > message.LockedUntilUtc.AddSeconds(-10))
{
// If so, we repeat the message
await message.RenewLockAsync();
}
}
}, brokeredMessageRenewCancellationTokenSource.Token);
string sandpKey = message.Properties["sandp"].ToString();
string dateKey = message.Properties["date"].ToString();
string symbolclassKey = message.Properties["symbolclass"].ToString();
string stockdataKey = message.Properties["stockdata"].ToString();
string stockcomparedataKey = message.Properties["stockcomparedata"].ToString();
var sandpTask = cache.GetAsync<List<StockData>>(sandpKey);
var dateTask = cache.GetAsync<DateTime>(dateKey);
var symbolinfoTask = cache.GetAsync<SymbolInfo>(symbolclassKey);
var stockdataTask = cache.GetAsync<List<StockData>>(stockdataKey);
var stockcomparedataTask = cache.GetAsync<List<StockMarketCompare>>(stockcomparedataKey);
await Task.WhenAll(sandpTask, dateTask, symbolinfoTask,
stockdataTask, stockcomparedataTask);
List<StockData> sandp = sandpTask.Result;
DateTime date = dateTask.Result;
SymbolInfo symbolinfo = symbolinfoTask.Result;
List<StockData> stockdata = stockdataTask.Result;
List<StockMarketCompare> stockcomparedata = stockcomparedataTask.Result;
StockRating rating = performCalculations(symbolinfo, date, sandp, stockdata, stockcomparedata);
if (rating != null)
{
saveToTable(rating);
await message.CompleteAsync();
}
else
{
Console.WriteLine("Message " + message.MessageId + " Completed!");
await message.CompleteAsync();
}
}
}
catch (TimeoutException time)
{
Console.WriteLine(time.Message);
}
catch (MessageLockLostException locks)
{
Console.WriteLine(locks.Message);
}
catch (RedisConnectionException redis)
{
Console.WriteLine("Start the redis server service!");
}
catch (MessagingCommunicationException communication)
{
Console.WriteLine(communication.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
finally
{
// Lock is stopped renewing task
brokeredMessageRenewCancellationTokenSource.Cancel();
}
}