简体   繁体   中英

Multiple responses for a single request in NServiceBus reaches the destination only after the completing the execution of Handle method

To increase the responsiveness of the application the Message handler is responding with multiple responses. But its observed that the messages are delivered to the destination only after the message handling is fully completed even though the transaction is disabled as follows var busConfiguration = new BusConfiguration(); busConfiguration.Transactions().Disable();

we have the code as follows: using MyMessages; using NServiceBus; using System;

    public class RequestDataMessageHandler : IHandleMessages<RequestDataMessage>
    {
        public IBus Bus { get; set; }

        public void Handle(RequestDataMessage message)
        {

            Console.WriteLine("Received request {0}.", message.DataId);
            Console.WriteLine("String received: {0}.", message.String);

            var response1 = new DataResponseMessage
            {
                DataId = message.DataId,
                String = "InProgress"
            };

            Bus.Reply(response1);


            try
            {
                TimeConsumingActivity();
                var response = new DataResponseMessage
                {
                    DataId = message.DataId,
                    String = message.String
                };

                Bus.Reply(response);
            }
            catch
            {
                var response2 = new DataResponseMessage
                {
                    DataId = message.DataId,
                    String = "Error in the process"            
                };
                Bus.Reply(response2);
            }
        }
    }

but its observer that response1 and response reaches destination only after TimeConsumingActivity is done (This is not the problem of the order)

Try:

 busConfiguration.Transactions().DoNotWrapHandlersExecutionInATransactionScope();

So, this is to the best of my understanding. There are several settings relating to transactions in NSB configuration, and I'm not sure I can definitively describe the differences between them.

The setting above should should prevent your handlers from being wrapped in a TransactionScope, which (if not disabled) could account for reply message not getting "released" to the bus until the transaction is completed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM