MQ Determine Successful vs Error Response on Temp Queue

It is unclear to me looking at the unit tests how to identify a successful response vs an error response when using a temp queue. For instance, using the GenericService, if the ID was decided at run time, how do you handle both the ErrorResponse and the GenericResponseDto from the temporary queue?

Works if response is always an exception:

public class GenericService : IService
{
    public GenericResponseDto Post(GenericRequest request)
    {
        if (request.Id % 2 == 0)
        {
            return new GenericResponseDto { RequestId = request.Id };
        }
        else
        {
            throw new Exception("Not divisible by 2");
        }
    }
}
using (var mqClient = MessageService.CreateMessageQueueClient())
{
    string replyToMq = mqClient.GetTempQueueName();
    var requestDto = new GenericRequest { Id = 1 };

    mqClient.Publish(new Message<GenericRequest>(requestDto)
    {
        ReplyTo = replyToMq
    });

    IMessage<ErrorResponse> responseMsg = mqClient.Get<ErrorResponse>(replyToMq);
    mqClient.Act(responseMsg);
}

Works if response is always successful (response dto):

using (var mqClient = MessageService.CreateMessageQueueClient())
{
    string replyToMq = mqClient.GetTempQueueName();
    var requestDto = new GenericRequest { Id = 2 };

    mqClient.Publish(new Message<GenericRequest>(requestDto)
    {
        ReplyTo = replyToMq
    });

    IMessage<GenericResponseDto> responseMsg = mqClient.Get<GenericResponseDto>(replyToMq);
    mqClient.Act(responseMsg);
}

What am I missing?

Your GenericResponseDto should have a ResponseStatus property, i.e:

public class GenericRequestDto : IPost, IReturn<GenericResponseDto> {  ... }

public class GenericResponseDto 
{
    public ResponseStatus ResponseStatus { get; set; }
}

Which you’d check to determine if it was an error, e.g:

var responseMsg = mqClient.Get<GenericResponseDto>(replyToMq);

var errorStatus = responseMsg.GetBody().ResponseStatus;
if (errorStatus != null) {
    //... Error!
}

Note: Your Request DTO is GenericRequestDto but you’re sending a GenericRequest.

1 Like