0
votes

We are using Azure Service Bus in our project and while reading messages from service bus topic/subscription. We are using subscriptionClient.OnMessageAsync event in conjunction with onMessageOptions.ExceptionReceived. Let me write down the steps we followed to reproduce the issue we are facing.

  1. Create a service bus namespace with default config in the azure portal
  2. Create a topic inside it with default config in the azure portal
  3. Create a subscription inside it with default config in the azure portal
  4. Create a console app and paste the code added below
  5. Connect the service bus using Service Bus Explorer
  6. Run the console app
  7. Send a few test messages from service bus explorer & watch the console app window
  8. Though the messages are processed successfully every time the control is going inside the ExceptionReceived method

Here's the code:

class Program
{
    static void Main()
    {
        var subscriptionClient = SubscriptionClient.CreateFromConnectionString
        (
            "servicebusendpointaddress",
            "topicname",
            "subscriptionname",
            ReceiveMode.PeekLock
        );

        var onMessageOptions = new OnMessageOptions();
        onMessageOptions.ExceptionReceived += OnMessageError;

        subscriptionClient.OnMessageAsync(OnMessageReceived, onMessageOptions);

        Console.ReadKey();
    }

    private static void OnMessageError(object sender, ExceptionReceivedEventArgs e)
    {
        if (e != null && e.Exception != null)
        {
            Console.WriteLine("Hey, there's an error!" + e.Exception.Message + "\r\n\r\n");
        }
    }

    private static async Task OnMessageReceived(BrokeredMessage arg)
    {
        await arg.CompleteAsync();
        Console.WriteLine("Message processing done!");
    }
}

Error

Are we missing something here?

Also one point to mention is that is we enable ‘autocomplete’ and remove the await arg.CompleteAsync(); then this is not happening.

var onMessageOptions = new OnMessageOptions() { AutoComplete = true};

In both the cases the messages are being processed successfully & removed from the subscription immediately.

1

1 Answers

0
votes

You might be getting this because you are debugging and stepping though the code i.e. the lock expires. The LockDuration by default is 60 seconds.

You can try setting your OnMessageOptions() like this to test:

var onMessageOptions = new OnMessageOptions() { AutoRenewTimeout = TimeSpan.FromMinutes(1) };