I am investigating NServiceBus and I am unsure how (or even if) I could use it to handle this scenario:
I have multiple clients sending work requests, which the distributor farms out to workers. The work will take a long time to complete and I would like the workers to report progress back to the client that sent the original request.
I have looked at the full duplex sample and also how to add the distributor to that sample. I've got these working, but when I modify them to reply with a series of progress messages (with a delay between the messages, as per code shown below), the client receives all the progress messages at the same time.
public class RequestDataMessageHandler : IHandleMessages<RequestDataMessage>
{
public IBus Bus { get; set; }
public void Handle(RequestDataMessage message)
{
for (var i = 0; i < 10; i++)
{
var count = i;
var response = this.Bus.CreateInstance<DataResponseMessage>(m =>
{
m.DataId = message.DataId;
m.Progress = count * 10;
});
this.Bus.Reply(response);
Thread.Sleep(1000);
}
}
}
I suspect I've not understood something basic about how NServiceBus works. Could someone explain where I've gone wrong, or point me at some examples and/or documentation?
What you have constructed will always send the messages as part of the same transaction. Since there is one transaction per handler, you won't be able to communicate progress this way. You would have to have a separate endpoint for each chunk of processing that would communicate progress. We've implemented communicating progress by updating something externally that is not involved in the transaction. That could be done by sending a non-transactional message to another endpoint to update progress or something like an RPC call. From there you could have something poll that progress data store.