Luis Valencia - MVP Luis Valencia - MVP - 2 months ago 13
C# Question

Cant debug Using QueueClient.OnMessage on a console app

I need to be able to debug the following console application to know if the messages are correctly deserialized or not.

However, when I press F5 on visual studio, I can debug this line:

string mensaje = message.GetBody();

but the following line seems is not executed, and the console APP closes and I can't even see the message I wrote to the console. My best guess its because the nature of async calls with the OnMessage thing. but I am not sure.

class Program
{
static void Main(string[] args)
{
try
{
string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
Console.WriteLine("Press key to continue");
Console.ReadKey();
QueueHelper.ReceiveMessageEmpresa("Empresa", connectionString);
}
catch (Exception ex)
{
throw ex;
}
}
}

public static void ReceiveMessageEmpresa(string queuName, string connectionString)
{
QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "Empresa");

// Configure the callback options
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(1);

// Callback to handle received messages
Client.OnMessage((message) =>
{
try
{
string mensaje = message.GetBody<string>();
Empresa empresa = JsonConvert.DeserializeObject<Empresa>(mensaje);
// Process message from queue
//Console.WriteLine("Body: " + );
Console.WriteLine("MessageID: " + message.MessageId);
Console.ReadKey();

// Remove message from queue
message.Complete();
}
catch (Exception ex)
{
// Indicates a problem, unlock message in queue
message.Abandon();
}
}, options);
}


or how can I rewrite this method to return the object? or what would you suggest?

Answer

We've run into this exact problem this afternoon and after taking code out line by line found that it's something to do with using Newtonsoft. If you comment out your line:

Empresa empresa = JsonConvert.DeserializeObject<Empresa>(mensaje);

You should find that it works.

We've got round this by wrapping the JsonConvert functionality in another class which we're calling from our OnMessage method.

I've no idea why this is the case unfortunately, but I hope this helps!

Comments