Can I incorporate both SignalR and a RESTful API?

后端 未结 3 1673
春和景丽
春和景丽 2020-11-29 14:53

I have a single page web app developed using ASP.NET. I recently converted many of the web methods to be push based, using the SignalR library. This really sped up the pag

3条回答
  •  無奈伤痛
    2020-11-29 15:38

    SignalR is actually already incorporated into WebAPI source vNext (4.1).

    If you use not the RTM build, but instead grab a build off Codeplex, you'd see there is a new project there called System.Web.Http.SignalR which you can utilize. It was added a couple of days ago with this commit - http://aspnetwebstack.codeplex.com/SourceControl/changeset/7605afebb159

    Sample usage (as mentioned in the commit):

    public class ToDoListController : HubController
    {
        private static List _items = new List();
    
        public IEnumerable 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 do not want to switch to vNext for now, you could always just use that code for reference.

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

提交回复
热议问题