Ich versuchte zu sehen, wie die Nachricht von/innerhalb der ExceptionReceived
Rückruf erhalten, aber die Informationen sind nicht in den Argumenten vorhanden. Hier ist mein Beispielcode:
var connectionString = "my-connection-string";
var queueName = "my-queue-name";
var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
var options = new OnMessageOptions();
options.ExceptionReceived += (sender, args) =>
{
// Neither sender or args contains information about the message being processed
};
client.OnMessage(brokeredMessage =>
{
throw new Exception();
}, options);
Je nach Bedarf, die einfache Lösung ist der Rückruf in einem try/catch wickeln:
client.OnMessage(brokeredMessage =>
{
try
{
// Process the message
...
// Complete the message (depends on the)
brokeredMessage.Complete();
}
catch (Exception ex)
{
Trace.TraceError("Exception captured: " + ex.Message);
// Here you have access to the brokeredMessage so you can log what you want.
...
//Abandon the message so that it could be re-process ??
brokeredMessage.Abandon();
}
}, options);
Nachdem Sie diese Wrapper in Funktion setzen können, oder erstellen eine Erweiterungsmethode für QueueClient
:
public static class QueueClientExtensions
{
public static void OnCustomMessage(this QueueClient queueClient, Action<BrokeredMessage> callback,
OnMessageOptions onMessageOptions)
{
queueClient.OnMessage(message =>
{
try
{
// process the message
callback(message);
//complete if success
message.Complete();
}
catch (Exception ex)
{
// Here you have access to the brokeredMessage so you can log what you want.
// message.GetBody<string>()
Trace.TraceError("Exception captured: " + ex.Message);
//Abandon the message so that it could be re-process
message.Abandon();
}
}, onMessageOptions);
}
}
Und rufen Sie diese Methode auf die gleiche Weise:
var queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName);
queueClient.OnCustomMessage(brokeredMessage =>
{
// Process the message
...
}, new OnMessageOptions());
Können Sie einfach versuchen, eine Ausnahme auszulösen, um zu sehen, ob Ihre Methode erreicht ist? – Thomas
Wenn ich eine Ausnahme in den Callback-Code wirke, dann wird der LogErrors ausgeführt, aber mein Problem ist, wie Sie die Nachricht, die im Callback verarbeitet wird, entweder mit den LogErrors-Parametern oder auf andere Weise ... –
Was haben Sie in der Absenderparameter? Sie sollten in der Lage sein, etwas in einem der Parameter zu finden – Thomas