在ASP.NET的Web API回调方法 [英] Callback methods in ASP.NET Web API

查看:210
本文介绍了在ASP.NET的Web API回调方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Web API服务器,具有不同平台的不同应用程序进行通信。现在我想创建将是这样一个回调的方法:客户端应用程序订阅它,并等待,直到服务器触发的消息

I have an ASP.NET Web API server, that have to communicate with different applications on different platforms. And now I want to create a method that would be something like a callback: client application subscribes to it and waits until server fires a message.

示例:
许多用户都在等待,直到新产品将在商店可用 - 他们订阅此事件。当产品到达在商店 - 每个用户接收的消息,这在某些情况下进行处理。

Example: Many users are waiting until new product will be available in store - they subscribe to this "event". When product arrives in store - every customer receives a message, which have to be handled in some case.


  1. 的用户发送一个请求,订阅

  2. 服务器收到请求产品可用!

  3. 服务器发送用户的每一个消息,产品的详细信息。

  4. 用户的应用程序处理的消息

我试图找到有关的ASP.NET Web API回调或双面一些信息,但一个建议 - 最好还是使用WCF这种方法

I tried to find some information about callbacks or duplex in ASP.NET Web API, but the one advice - it's better to use WCF for this approach.


  • 在每个客户端应用程序创建类似计时器,每N秒发送一个请求是可用的产品?直到获得假。当响应将是真实的 - 发送邮件获取产品的详细信息。它会导致大量的流量,如果会有很多客户,这些计时器 - 这将是一件坏事,是不是

  • 创建一个小的回调服务器(也许WCF)。但是,在这种情况下,将是一个很大的与此服务器和应用程序在不同平台之间的通信问题。

  • 也许有中的ASP.NET Web API的一些解决,我错过了。

如果你有一些想法如何,我可以解决这个问题,请给我一个建议。

If you have some ideas how i can solve this problem, please give me an advice.

感谢您的帮助。

推荐答案

看起来你想从你的服务器推送通知 - 这,在这种情况下,可以通过SignalR使用Web API相结合来实现。

Looks like you want push notifications from your server - which, in this case, can be achieved by combining SignalR with Web API.

布拉德·威尔逊有一个很好的例子:

Brad Wilson has a great example on this:


  1. code在这里 - <一个href=\"https://github.com/bradwilson/WebstackOfLove\">https://github.com/bradwilson/WebstackOfLove

NDC奥斯陆的谈话解释这一切 - http://vimeo.com/43603472

NDC Oslo talk explaining all this - http://vimeo.com/43603472

总之,当你添加新的项目到Web API,可以通知所有连接(订阅)客户端:

In short, whenever you add new item to the Web API, you can notify all the connected (subscribed) clients:

public void PostNewItem(ToDoItem item)
        {
            lock (db)
            {
                // Add item to the database
                db.Add(item);

                // Notify the connected clients
                Hub.Clients.processItem(item);
            }
        }

SignalR将调用 processItem 功能在客户端上。

另外,你可能想看看成JavaScript SSE和Web API PushStreamContent 但这是更加低的水平,SignalR抽象了很多这种类型的东西给你,让它可能是更复杂的处理。

Alternatively, you might want to look into JavaScript SSE and Web API PushStreamContent but that is much more low level and SignalR abstracts a lot of this type of stuff for you so it might be more complicated to deal with.

我的博客上讲述这个方法在这里<一个href=\"http://www.strathweb.com/2012/05/native-html5-push-notifications-with-asp-net-web-api-and-knockout-js/\">http://www.strathweb.com/2012/05/native-html5-push-notifications-with-asp-net-web-api-and-knockout-js/

I blogged about this approach here http://www.strathweb.com/2012/05/native-html5-push-notifications-with-asp-net-web-api-and-knockout-js/

这篇关于在ASP.NET的Web API回调方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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