As I’ve started to use MassTransit with SignalR, one of the things that annoyed me was the hoops I had to jump through to get a nice asynchronous request from SignalR into MassTransit. There was a lot of plumbing since the MT did not support the TPL.
Well, I’ve changed that. With version 2.5.3 (a prerelease version on NuGet), you can now get a really nice clean syntax to return tasks from server-side SignalR hubs (and other calls that expect a Task return value.
Shown above is a SignalR hub that sends a request message off to some service. The LocationResult handler that is added within the closure returns a Task<LocationResult>, which can be returned to SignalR allowing the server-side code to remain asynchronous. If additional work needed to be done to transform the message to another type or do perform some type of validation, a .ContinueWith() could be added to the task to return the proper result type.
If the request times out, the task for the handler will be cancelled, so be sure to take that into account.
Pretty power stuff eh?