如何防止HttpWebRequest超时响应Azure Web App超时 [英] How To Prevent HttpWebRequest with long response from timing-out Azure Web App

查看:143
本文介绍了如何防止HttpWebRequest超时响应Azure Web App超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在C#中使用HttpWebRequest从Azure Web App中的Internet资源获取数据.问题在于,Azure在使连接保持活动状态方面有一个限制(

下面是一些示例代码来说明:

        webRequest = WebRequest.Create(PAGE_URL) as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.CookieContainer = cookies;
        webRequest.Timeout = Timeout.Infinite;
        webRequest.KeepAlive = true;
        StreamWriter requestWriter2 = new
            StreamWriter(webRequest.GetRequestStream());
        requestWriter2.Write(postString); 
        requestWriter2.Close();
        WebResponse response = webRequest.GetResponse();
        Stream stream = response.GetResponseStream();

添加webRequest.Timeout和webRequest.KeepAlive不能解决问题.

​​此线程上的

jbq提到他通过发送换行符"来解决每5秒钟",但是没有解释如何准确地完成此操作.他在回答有关Azure VM的问题,但我认为Azure Web App在负责超时的负载平衡器方面将具有类似的行为.

问题: 我如何发送一个HttpWebRequest,然后在上一个正在运行的同时发送另一个HttpWebRequest,并保持空白并保持连接并防止Azure负载均衡器使天青应用程序超时?是否需要使用新的会话变量?也许是异步方法?我是否需要在主请求之前 发送"ping"请求?如果是这样,在实施过程中将如何看待?还是完全其他的东西?请提供一些源代码作为示例:)

注意:您不需要不需要使用HttpWebRequest来复制此问题.将调试器从Visual Studio中附加到实时Azure Web App.在任何代码段中在Visual Studio中放置一个断点.当达到该断点时,大约4分钟后,您将看到浏览器中的页面停止加载,并且空白处显示空白.因此,它与HttpWebRequest并不特别相关,但是由于某些响应时间超过4分钟,因此该操作通常会导致此类问题.

* 我认为我正在寻找的是异步方法的实现.找到满意的实现后,我将更新此帖子.

解决方案

如果您要向Azure网站进行 HttpWebRequest ,则可以在使用HttpWebRequest的客户端代码上使用ServicePointManager.SetTcpKeepAlive.

https://msdn.microsoft.com/zh-CN/library/system.net.servicepointmanager.settcpkeepalive(v=vs.110).aspx

您要谈论的4分钟超时是TCP层上的 IDLE超时,设置此设置将确保您的客户端(正在使用HttpWebRequest)通过TCP发送ACK数据包,以便连接不会闲着.

如果您的Web应用程序正在向其他服务发出HttpWebRequest,则您仍然可以使用此功能,但这将确保在调用远程服务时不触发空闲超时.您对Azure Web应用程序的实际HTTP请求可能仍然会花费4分钟的时间,如果Azure Web应用程序的客户端不是HttpWebRequest,那么4分钟的空闲超时将再次咬住您...

此处最好的做法是稍微更改代码以实现JOB模型类型的模式,在其中您进行服务器调用,并返回一个JOBID.然后,客户端以轮询方式使用此JOBID查询服务器,并且当服务器上的作业完成时,可以将该JOBID的状态设置为COMPLETED,在这种情况下,客户端可以检索数据.您可以在Azure Webapps中使用Webjobs来实现类似的目的.

希望这对您有帮助...

We are using an HttpWebRequest in C# to get data from an internet resource in our Azure Web App. The problem is that Azure has a limitation on how long it keeps the connection alive (around 240 seconds). Due to the nature of our application, the response will sometimes take longer than 240 seconds. When this happens, the webpage will go white, and the "View Source" will show zero source code (which has made this issue difficult to debug).

Here's some sample code to illustrate:

        webRequest = WebRequest.Create(PAGE_URL) as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.CookieContainer = cookies;
        webRequest.Timeout = Timeout.Infinite;
        webRequest.KeepAlive = true;
        StreamWriter requestWriter2 = new
            StreamWriter(webRequest.GetRequestStream());
        requestWriter2.Write(postString); 
        requestWriter2.Close();
        WebResponse response = webRequest.GetResponse();
        Stream stream = response.GetResponseStream();

Adding webRequest.Timeout and webRequest.KeepAlive did not solve the issue.

jbq on this thread mentioned he had a workaround by sending a "newline character every 5 seconds", however did not explain how to accomplish this exactly. He was answering a question about an Azure VM, but I think an Azure Web App would have similar behaviors with respect to what I believe are the load balancers that are responsible for the timeout.

The Question: How can I send one HttpWebRequest, and then send another HttpWebRequest while the previous one is running with a blank line to maintain the connection and prevent the Azure load balancer(?) from timing out the azure application? Would a new session variable need to be used? Perhaps an asynchronous method? Do I need to send the "pinging" request before the main request? If so, how would this look in implementation? Or is it something else entirely? Please provide some source code as an example :)

Note: you do not need to use an HttpWebRequest to replicate this issue. Attach a debugger from within Visual Studio to a live Azure Web App. Place a breakpoint within Visual Studio at any piece of code. When that breakpoint is hit, after roughly 4 minutes you'll see the page in your browser stop loading and go white with an empty source. So, it's not specifically related to HttpWebRequest, but that is an operation that would typically cause this sort of issue since some responses take longer than 4 minutes.

*EDIT: I think what I am looking for is an implementation of Asynchronous methods. I will update this post as I find a satisfactory implementation.

解决方案

If you are making an HttpWebRequest to an Azure Website then you use ServicePointManager.SetTcpKeepAlive on your client code which uses HttpWebRequest.

https://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.settcpkeepalive(v=vs.110).aspx

The 4 minute timeout that you are talking about is an IDLE timeout over the TCP layer and setting this will ensure that your client (who is using HttpWebRequest) sends ACK packet over TCP so that the connection doesn't get idle.

If your web application is making a HttpWebRequest to some other service, you can still use this function but that will just ensure that the Idle timeout is not hit when calling the remote service. Your actual HTTP request to the Azure Webapp may still hit the 4 minute time and if the client to your Azure web app is not HttpWebRequest, then again the 4 minute idle timeout will bite you...

The best thing to do here is to change the code a bit to implement a JOB Model kind of pattern where-in you make a server call which returns a JOBID. The client then queries the server using this JOBID in a polling fashion and when the job completes on the server the status of this JOBID can be set to COMPLETED in which case the client can then retrieve the data. You can use Webjobs in Azure Webapps to achieve something like this.

Hope this helps...

这篇关于如何防止HttpWebRequest超时响应Azure Web App超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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