snatch-dev / Chronicle

Implementation of saga pattern for .NET Core
MIT License
397 stars 70 forks source link

CompensateAsync not fired in When using saga inside Azure Service Bus handler #60

Open abuzaforfagun opened 3 years ago

abuzaforfagun commented 3 years ago

I have tried to use Chronicle ProcessAsync from Azure Service Bus message handler, like below: ` public void RegisterQueueHandler(string primaryKey, string queueName) where T : class { var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 1, AutoComplete = false }; _queueClient = new QueueClient(primaryKey, queueName); _queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions); }

    private async Task ProcessMessagesAsync<T>(Message message, CancellationToken token) where T : class
    {
        using var serviceProviderScope = _serviceProvider.CreateScope();
        var payload = JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(message.Body));

        if (payload is ISagaCommand)
        {
            var sagaCoordinator = serviceProviderScope.ServiceProvider.GetRequiredService<ISagaCoordinator>();
            await sagaCoordinator.ProcessAsync(payload, null);
        }
        else
        {
            var mediator = serviceProviderScope.ServiceProvider.GetRequiredService<IMediator>();
            await mediator.Send(payload, token);
        }

        await _queueClient.CompleteAsync(message.SystemProperties.LockToken);
    }

`

When any exception happens inside saga HandleAsync, CompensateAsync is ignored and ExceptionReceivedHandler fired.