MassTransit v2.5.3 Now Supports the TPL

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.

public class Location :
    Hub
{
    public Task GetLocation(string truckId)
    {
        Task task = null;
        Bus.Instance.PublishRequestAsync(new GetLocation { TruckId = truckId }, x =>
        {
            task = x.Handle(message => {});
            x.SetTimeout(30.Seconds());
        });

        return task;
    }
}

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.

public class Location :
    Hub
{
    public Task GetLocation(string truckId)
    {
        Task task = null;
        Bus.Instance.PublishRequestAsync(new GetLocation { TruckId = truckId }, x =>
        {
            task = x.Handle(message => {});
            x.SetTimeout(30.Seconds());
        });

        return task.ContinueWith(t => new LocationView(...));
    }

    public class LocationView
    {
        public string TruckId { get; set; }
        public string Location { get; set; }
    }
}

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?

Related Articles:

    Post Footer automatically generated by Add Post Footer Plugin for wordpress.

    About Chris Patterson

    Chris is a senior architect for RelayHealth, the connectivity business of the nation's leading healthcare services company. There he is responsible for the architecture and development of applications and services that accelerate care delivery by connecting patients, providers, pharmacies, and financial institutions. Previously, he led the development of a new content delivery platform for TV Guide, enabling the launch of a new entertainment network seen on thousands of cable television systems. In his spare time, Chris is an active open-source developer and a primary contributor to MassTransit, a distributed application framework for .NET. In 2009, he was awarded the Most Valuable Professional award by Microsoft for his technical community contributions.
    This entry was posted in masstransit, signalR. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
    • Daniel Marbach

      Hy chris
      This might a be a missleading example. Why would you even use Masstransit for something which can be clearly categorized as a query?
      Maybe the sample should describe a command on which the client wants success or error status?
      Also I prefer to have clearer semantics and I try to avoid “publish” for commands. You only “send” commands

      Daniel

      • http://twitter.com/PhatBoyG Chris Patterson

        Well, it was a contrived example that we built as part of the presentation at a user group meeting in early June. On how they could really push truck location updates to a monitoring screen in real time. The query was just a part of doing the demo without building an entirely new web site.