private async Task StopReceiveMessagesInternal(CancellationToken cancellationToken = default) { if (_serviceBusSubscriberProcessor != null) { try { await _serviceBusSubscriberProcessor.CloseAsync(cancellationToken); await _serviceBusSubscriberProcessor.DisposeAsync(); } catch (Exception ex) { _logger.LogError(ex, "Service Bus receive messages stopping failure"); } finally { _serviceBusSubscriberProcessor = null; } } }
public async Task StartReceiveMessages( string queueOrTopicName, string subscriptionName = default, Func <string, string, object, Task> processMessageFunc = default, Func <string, string, Exception, Task> processErrorFunc = default, ServiceBusSubscriberReceiveOptions options = default, CancellationToken cancellationToken = default) { if (_serviceBusSubscriberProcessor != null) { _logger.LogError("Service Bus processor already initialized. Stop it first"); throw new ServiceBusSubscriberOperationException( "Service Bus processor already initialized. Stop it first."); } try { _serviceBusSubscriberProcessor = new ServiceBusSubscriberProcessor(_client, queueOrTopicName, subscriptionName, options) { QueueOrTopicName = queueOrTopicName, SubscriptionName = subscriptionName, ClientProcessMessageFunc = processMessageFunc, ClientProcessErrorFunc = processErrorFunc }; _serviceBusSubscriberProcessor.ProcessMessageAsync += ProcessMessageInternal; _serviceBusSubscriberProcessor.ProcessErrorAsync += ProcessErrorInternal; await _serviceBusSubscriberProcessor.StartProcessingAsync(cancellationToken); } catch (Exception ex) { await StopReceiveMessagesInternal(cancellationToken); _logger.LogError(ex, "Service Bus start receive messages from topic subscription failure"); throw new ServiceBusSubscriberOperationException( "Service Bus start receive messages from topic subscription failure", ex); } }