3

Just wondering, if there's existed class of framework, which would deal with request queuing.

When request is sent, is should be added to the queue. If something is wrong (no internet), request should be stored, and tried to be sent again later.

Meanwhile, more requests can be created. If there's still no network, all new requests should be stored in the queue, and sent again while network would return.

Going to make same functional, wondering if it is already implemented.

EDIT1: I'm using HttpWebRequest for Posting/Getting json data to/from server.

EDIT2: Sample case:

  1. User press Button1. First request is sent, added to queue, (internet is avaivable), app got answer, request is removed from the queue.

  2. User press Button2. Second request is sent, added to queue, no internet connection -> request is stored in the queue.

  3. User press Button3. Third request is going to be send, but as queue is not empty, it is just stored to the queue. Request2 tries to be delivered, but still no connection.

  4. Some time later, connection is established again, so Request2 succeeded, removed from the queue, then Request3 also succeeded and removed from the queue.

  5. What is important - it should be resistant to tombstoning. If phone goes to sleep before calling request, it should be stored. Is request is in progress, it should be cancelled (or exactly this message should not be stored).

EDIT3: For avoiding delivery exceptions, i'm using this wrapper:

public async Task<string> GetAsync(string url)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.Method = HttpMethod.Get;
        httpWebRequest.Accept = "application/json";

        try
        {
            var response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();

            if (response == null || response.StatusCode != HttpStatusCode.OK)
                return String.Empty;

            string data;

            using (var responseStream = response.GetResponseStream())
            {
                using (var postStreamReader = new StreamReader(responseStream))
                {
                    data = await postStreamReader.ReadToEndAsync();
                    postStreamReader.Close();
                }
                responseStream.Close();
            }

            return data ?? String.Empty;
        }
        catch (Exception ex)
        {
            return String.Empty;
        }
    }

It returns String.Empty if there's any exceptions, otherwise, it returns server answer's string. What i need now is a pattern, which would guarantee that call was successful (result is not empty), otherwise, keep it in queue (to keep order) and would try to raise call again, until it would be delivered.

EDIT4: for now, i'm considering having a queue in a semaphore, and call it when i need to force pushing. Any thoughts if it would work fine?

4

4 回答 4

3

我做了一个简单的例子,它使用 Queue 对象来保存请求的 URL 列表。基本上它只是一一提出请求。并且总是在请求完成时发送 Completed 事件。然后由 Completed 事件的处理程序来处理返回的页面或返回的错误条件。

此外,还有墓碑支持。基本上,当调用 Application_Deactivated 或 Application_Closing 回调时,它将队列保存到隔离存储中。如果应用程序从墓碑阶段返回,则将其加载到应用程序构造函数/Application_Activated 函数中。我希望这能让你开始。

http://www.mediafire.com/download/cncuwx1kttehv8y/PhoneApp15.zip

于 2013-10-23T14:49:55.313 回答
2

抱歉,我没有时间给出一个非常完整的答案,但是 System.Threading.Tasks.Dataflow 命名空间可能对您有一些非常有用的东西。

于 2013-11-12T00:17:44.780 回答
1

看看使用 AJAX 请求在客户端运行的 SPA(单页应用程序)。您的服务器逻辑将从处理所有业务逻辑转变为简单地向客户端数据提供在客户端上用 JavaScript 完成的业务逻辑。这允许应用程序在没有服务器数据进入的情况下运行。然后,您可以在出现 404 错误的情况下处理您希望它执行的操作,包括稍后重试。非常熟悉 JavaScript。

于 2013-06-02T00:15:04.130 回答
0

这只是从记忆中弹出来的。但不确定是否以及如何实现持久性。如果即使在浏览器重新启动后仍需要传递消息,您可以使用本地存储来实现该部分。

Stomp作为协议可以满足您的需求。在这里很好看:http : //blog.mayflower.de/2011-Message-Queues-for-web-applications-with-STOMP.html

其他选项: 用于 jQuery 的 Messagebus

一个完整的服务总线可能有点矫枉过正,但借用一些想法,它可以提供一个先机:

pServicebus用 C# 实现服务器端并附带 JS-client

MassTransit 的 JS客户端:MassTransit-JS

于 2013-10-21T14:35:07.190 回答