C# 使用OnMessageOptions中接收的ExceptionReceived

C# 使用OnMessageOptions中接收的ExceptionReceived,c#,azure,azureservicebus,azure-servicebus-queues,C#,Azure,Azureservicebus,Azure Servicebus Queues,我正在尝试使用方法QueueClient.OnMessage(Action, OnMessageOptions)。在OnMessageOptions中,我编码了这个简单的异常接收事件: void LogErrors(object sender, ExceptionReceivedEventArgs e) { if (e.Exception != null) { Trace.TraceError("Exception captured: " + e.Exception.Messag

我正在尝试使用方法
QueueClient.OnMessage(Action, OnMessageOptions)
。在
OnMessageOptions
中,我编码了这个简单的
异常接收事件:

void LogErrors(object sender, ExceptionReceivedEventArgs e)
{
  if (e.Exception != null)
  {
    Trace.TraceError("Exception captured: " + e.Exception.Message);
  }
}

我的疑问是如何获取回调中正在处理的
BrokeredMessage
中的“LogErrors”,以便在发生BrokeredMessages时保存带有错误的日志。

我尝试查看如何从
ExceptionReceived
回调中获取消息,但参数中没有信息。以下是我的示例代码:

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);
根据您的需要,简单的解决方案是将回调封装在try/catch中:

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);
在您可以将此包装器放入函数或为
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);
    }
}

你能不能试着抛出一个异常来看看你的方法是否达到了?当我在回调代码中抛出一个异常时,LogErrors就会被执行,但我的问题是如何使用LogErrors参数或其他方式获取回调中正在处理的消息…发送方参数中有什么?您应该能够在其中一个ParametersThank中找到一些东西,以确认ExceptionReceived回调中BrokeredMessage不可用。简单的试一试解决方案对我很好@AlbertAixendri,如果答案对你合适,你能接受吗?
var queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName);
queueClient.OnCustomMessage(brokeredMessage =>
{
    // Process the message
    ...
}, new OnMessageOptions());