what is difference between subscriptionClient.AbandonAsync vs subscriptionClient.closeAsync.
i need to check whether topic exist in subscriptionClient or not. I can not use azure management client because of some limitation
what is difference between subscriptionClient.AbandonAsync vs subscriptionClient.closeAsync.
i need to check whether topic exist in subscriptionClient or not. I can not use azure management client because of some limitation
1. what is difference between subscriptionClient.AbandonAsync vs subscriptionClient.closeAsync.
According to my research and test, the method subscriptionClient.AbandonAsync
is used to tell service bus that the message is abandoned and it needs to process the message again. Once the message's DeliveryCount reaches the MaxDeliveryCount, the message is moved to the DLQ, specifying the MaxDeliveryCountExceeded
reason code. For more details, please refer to the document
The method subscriptionClient.closeAsync
is used to close the subscriptionClient then your client cannot receive the message from the subscription.
My test:
Code:
namespace CoreReceiverApp
{
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;
class Program
{
const string ServiceBusConnectionString = "<your_connection_string>";
const string TopicName = "<your_topic_name>";
const string SubscriptionName = "<your_subscription_name>";
static ISubscriptionClient subscriptionClient;
public static async Task Main(string[] args)
{
subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName);
Console.WriteLine("======================================================");
Console.WriteLine("Press ENTER key to exit after receiving all the messages.");
Console.WriteLine("======================================================");
// Register subscription message handler and receive messages in a loop
RegisterOnMessageHandlerAndReceiveMessages();
Console.ReadKey();
await subscriptionClient.CloseAsync();
}
static void RegisterOnMessageHandlerAndReceiveMessages()
{
// Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc.
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
// Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity.
// Set it according to how many messages the application wants to process in parallel.
MaxConcurrentCalls = 1,
// Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
// False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
AutoComplete = false
};
// Register the function that processes messages.
subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
}
static async Task ProcessMessagesAsync(Message message, CancellationToken token)
{
// Process the message.
Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
// This can be done only if the subscriptionClient is created in ReceiveMode.PeekLock mode (which is the default).
await subscriptionClient.AbandonAsync(message.SystemProperties.LockToken);
Console.WriteLine(message.SystemProperties.DeliveryCount);
}
static Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
{
Console.WriteLine($"Message handler encountered an exception {exceptionReceivedEventArgs.Exception}.");
var context = exceptionReceivedEventArgs.ExceptionReceivedContext;
Console.WriteLine("Exception context for troubleshooting:");
Console.WriteLine($"- Endpoint: {context.Endpoint}");
Console.WriteLine($"- Entity Path: {context.EntityPath}");
Console.WriteLine($"- Executing Action: {context.Action}");
return Task.CompletedTask;
}
}
}
There are several options to validate a topic exists or not. I can think of the two most common ways that would be:
ManagementClient
.The difference between the two is what you use to authenticate your code. With the SDK library, you can use a connection string or a TokenProvider (Managed Identity or another way to authenticate). Wherewith the Management library you can only authenticate with Azure Active Directory (requires tenant ID, client ID, client secret, subscription ID, and resource group name).
In regards to what seems to be a completly separate question
what is difference between subscriptionClient.AbandonAsync vs subscriptionClient.closeAsync.
subscriptionClient.AbandonAsync()
abandons the message to allow another competing consumer to pick it up. Where subscriptionClient.closeAsync()
closes SubscriptionClient
's connection to the broker.