Ben Cameron Ben Cameron - 18 days ago 7
C# Question

Web API and SignalR - the correct way?

I am looking to start a project in which I need to make push updates from Web API.

I found this post which gives an example of how to do this.

Can I incorporate both SignalR and a RESTful API?

http://bradwilson.typepad.com/blog/2012/07/webstack-of-love.html

This demo uses a class called

ApiControllerWithHub<THub>

You extend your web api controllers from this.

This post is quite old now (2013). Is this still the correct way to do things or have there been developments in Web API to allow for push updates?

Answer

I don´t think there´s a new integration between web api controllers and hubs. That code is totally fine.

The key part is:

Lazy<IHubContext> hub = new Lazy<IHubContext>(
    () => GlobalHost.ConnectionManager.GetHubContext<THub>()
);

You can implement that code the way you want. But the sample code is a nice way to use it.

Comments