Can I enable both SignalR and RESTful API?

I have a one-page web application developed using ASP.NET. I recently converted many of the push-based web methods using the SignalR library. This greatly accelerated the page and reduced the number of server calls from the page.

At the same time, I also looked at RESTful ASP.NET WebAPI for some server-side methods, and the real beauty is that it allows you to create APIs for external applications at the same time. I am developing the main application (which will be important for what I do )

However, having looked at several articles and these two questions that use push and WebAPI methods, seem to be two completely different paradigms for client-server interaction. I'm sure I can create various methods that can be accessed through any protocol, but I'm not sure if there are pitfalls or if this is considered sloppy - maybe there is a more elegant way to achieve what I aim for.

There are, of course, situations where I want a RESTful WebAPI to broadcast events through a SignalR hub ... The opposite (SignalR ever needing access to a WebAPI) seems less likely, but I believe that it is still possible.

Has anyone done this? Does anyone have any tips or advice on how to proceed? What would be the most elegant way here?

+86
rest web-applications asp.net-web-api signalr
Sep 11 '12 at 11:34
source share
3 answers

Take a look at the video from this blog post . It explains exactly how you can use WebAPI with SignalR.

Essentially, the integration of the Web API + SignalR is in this class :

public abstract class ApiControllerWithHub<THub> : ApiController where THub : IHub { Lazy<IHubContext> hub = new Lazy<IHubContext>( () => GlobalHost.ConnectionManager.GetHubContext<THub>() ); protected IHubContext Hub { get { return hub.Value; } } } 

It's all.:)

+66
Sep 11 '12 at 12:26
source share

SignalR is actually already included in the WebAPI vNext (4.1) source.

If you are not using an RTM assembly, but instead grab the Codeplex assembly, you will see that there is a new project called System.Web.Http.SignalR that you can use. This was added a couple of days ago with this commit - http://aspnetwebstack.codeplex.com/SourceControl/changeset/7605afebb159

Using an example (as indicated in a commit):

 public class ToDoListController : HubController<ToDoListHub> { private static List<string> _items = new List<string>(); public IEnumerable<string> Get() { return _items; } public void Post([FromBody]string item) { _items.Add(item); // Call add on SignalR clients listening to the ToDoListHub Clients.add(item); } } 

If you don’t want to switch to vNext right now, you can always just use this code for reference.

This implementation is very similar (a little more polished, includes tests, etc.) to what Brad Wilson showed in NDC Oslo - http://vimeo.com/43603472

+12
Sep 11 '12 at 12:28
source share

Here is a video showing the integration of the two technologies http://channel9.msdn.com/Events/TechDays/Belgium-2013/25 and here is the NuGet package for integration https://www.nuget.org/packages/Microsoft.AspNet.WebApi .SignalR /

+1
Oct. 15 '13 at 7:31
source share



All Articles