request.GetResponse() 超时

request.GetResponse() Timeout

Waker.cs

class Waker
{
    Timer timer;
    public Waker()
    {
        timer = null;
    }
    public void WakeUpApplicationPool(object obj)
    {
        string url = "http://www.example.com";
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Program.LogToFile("WakeUpApplicationPool: " + response.StatusDescription);
            response.Close();
        }
        catch (Exception ex)
        {
            Program.LogToFile("WakeUpApplicationPool_Error: " + ex.ToString());
        }
    }
    public void Start()
    {
        TimerCallback callback = new TimerCallback(WakeUpApplicationPool);
        int DUE_TIME = 0; //The amount of time to delay before the callback parameter invokes its methods.
        int PERIOD = int.Parse(ConfigurationManager.AppSettings["WakerIntervalPeriod"]); //The time interval (miliseconds) between invocations of the methods referenced by callback
        timer = new Timer(callback, null, DUE_TIME, PERIOD);
    }
    public void Stop()
    {
        timer.Dispose();
    }

}

Program.cs:

static void Main(string[] args)
    {
        try
        {
            Waker waker = new Waker();
            waker.Start();
        }
        catch(Exception ex) 
        {
            LogToFile(ex.ToString());                
        }            
    }

日志文件:

15 Apr 2015 18:29:39 - WakeUpApplicationPool: OK

15 Apr 2015 18:31:39 - WakeUpApplicationPool: OK

15 Apr 2015 18:33:59 - WakeUpApplicationPool_Error: System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 205.144.171.35:80
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleReporting.Waker.WakeUpApplicationPool(Object obj)

15 Apr 2015 18:35:39 - WakeUpApplicationPool: OK

15 Apr 2015 18:37:39 - WakeUpApplicationPool: OK

15 Apr 2015 18:41:18 - WakeUpApplicationPool_Error: System.Net.WebException: The operation has timed out
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleReporting.Waker.WakeUpApplicationPool(Object obj)

15 Apr 2015 18:43:18 - WakeUpApplicationPool_Error: System.Net.WebException: The operation has timed out
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleReporting.Waker.WakeUpApplicationPool(Object obj)

15 Apr 2015 18:45:18 - WakeUpApplicationPool_Error: System.Net.WebException: The operation has timed out
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleReporting.Waker.WakeUpApplicationPool(Object obj)

15 Apr 2015 18:47:18 - WakeUpApplicationPool_Error: System.Net.WebException: The operation has timed out
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleReporting.Waker.WakeUpApplicationPool(Object obj)

问题是:

  1. 我的代码在遇到超时错误后无法运行。但是在我重新启动 Program.exe 之后,它又开始工作了,但是在 10 分钟后它遇到了超时错误。
  2. 我想使用这个 Program.exe 来唤醒托管在托管服务提供商处的应用程序池。
  3. 所以谁能告诉原因和解决办法是?我提到了 this,但它也不适用于我的代码

我将 WakerIntervalPeriod 设置为 10 分钟而不是 5 分钟后问题就解决了。