我可以二者结合SignalR和一个RESTful API? [英] Can I incorporate both SignalR and a RESTful API?

查看:315
本文介绍了我可以二者结合SignalR和一个RESTful API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用ASP.NET单个页面的Web应用程序开发。我最近转换的许多Web方法要基于推,使用 SignalR 库。这确实大大加快了页面,并减少了很多从页面调用服务器中。

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 page considerably and reduced a lot of the server calls from the page.

与此同时,我也一直在寻找的REST风格的ASP.NET的WebAPI对于一些服务器端方法,与真正的美在于它允许创建的同时外部应用程序的API我开发的核心应用程序(这将是我在做什么重要)。

At the same time, I've also been looking at the RESTful ASP.NET WebAPI for some of the server-side methods, with the real beauty being that it allows to create an API for external applications at the same time that I develop the core application (which will be important for what I'm doing).

看来,在看几篇文章后<一个href=\"http://stackoverflow.com/questions/11427573/is-it-possible-to-create-simple-rest-services-with-signalr\">these <一href=\"http://stackoverflow.com/questions/11646918/turn-by-turn-game-use-signalr-or-both-signalr-and-rest-api\">two问题,即推的WebAPI方法似乎是为客户端服务器通信两个完全不同的模式。我敢肯定,我可以创建可通过任一协议进行访问的各种方法,但我是否有陷阱,以这种不确定的,或者如果这被认为是草率的 - 也许有一个更优雅的方式来实现是我的目标对于。

It seems however, after looking at several articles and these two questions, that push and WebAPI methods seem like two entirely different paradigms for client-server communication. I'm sure that I can create various methods that can be accessed via either protocol, but I'm uncertain if there are pitfalls to this or if this is considered sloppy -- maybe there's a more elegant way to achieve what I'm aiming for.

有肯定,我想R​​EST风格的WebAPI广播通过SignalR枢纽事件的情况......与此相反的(SignalR曾经需要访问的WebAPI),似乎不太可能,但我想还是有可能的。

There are certainly situations in which I want the RESTful WebAPI to broadcast events via a SignalR hub... The opposite (SignalR ever needing to access the WebAPI) seems less likely, but I suppose still possible.

有没有人这样做呢?有没有人对如何进行任何意见或建议吗?什么是最优雅的方式前进吗?

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

推荐答案

看看视频从这个的博客文章。这正好说明了如何使用的WebAPI与SignalR。

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

从本质上讲,网络API + SignalR集成包括在这个<一个href=\"https://github.com/bradwilson/WebstackOfLove/blob/master/Controllers/ApiControllerWithHub.cs\">class:

Essentially, Web API + SignalR integration consists 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; }
    }
}

这就是全部。 :)

That's all. :)

这篇关于我可以二者结合SignalR和一个RESTful API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆