我在C#中开发了一个ASP.NET(.asmx)Web服务。在它返回之前,它启动一个线程(通过线程池和一个简单的线程尝试)来执行Web请求。在创建Web请求之前,我需要大约2秒的延迟。我已经尝试过睡眠,定时器,甚至是数百万的for循环计数。在我的开发服务器上,它始终有效。在客户的服务器上,它的工作时间不到10%。根据我的日志记录,线程在Web请求之前的延迟期间结束。什么可以杀死它? ASP.NET是否认为它不活跃?
它记录“TesterRequest:在WAIT之前”,然后“线程被中止”(见下面的代码),当然没有别的。
相关代码 - 用于循环版本(放入for循环以便在睡眠和计时器方法无法维持之后进行测试,在完成经过时间之前中止):
static void ThreadProc(object p_tcti)
{
// Calls a method inside the tester via HttpWebRequest
int nSleep = 606111000; // 2000;
LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest: Before WAIT of " + nSleep.ToString());
int nDummy = 0;
for (int n = 0; n < nSleep; n++)
{
nDummy = n;
}
LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest: After WAIT " + nDummy.ToString() + " creating web request to tester.");
HttpWebRequest reqWeb = (HttpWebRequest)WebRequest.Create(tcti.m_strURL);
LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest: Created the web request.");
reqWeb.Method = "GET";
//reqWeb.MaximumAutomaticRedirections = 4;
//reqWeb.MaximumResponseHeadersLength = 4;
reqWeb.UserAgent = "ABRWebService /1.0";
// Do it! This will block for up to 40 seconds
string strStatus = "";
reqWeb.Timeout = 40000;
try
{
HttpWebResponse resWeb = (HttpWebResponse)reqWeb.GetResponse();
// LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest: Received response from the tester web request. ");
if (resWeb.StatusCode != HttpStatusCode.OK)
{
strStatus = resWeb.StatusDescription;
LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest FAILED" + " " + strStatus);
}
resWeb.Close();
}
catch (WebException we)
{
// Ignore timeouts since we don't give a about the
// response.
tcti.m_strResult = we.Message;
if (we.Status != WebExceptionStatus.Timeout)
{
LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest Web Exception: " + we.Message + " Request: " +
tcti.m_strResult + ", URL: " + tcti.m_strURL);
if (!strStatus.Equals(""))
LogMessage("Status: " + strStatus);
LogMessage("TesterRequest FAILED" + " " + DateTime.Now);
// RWC don't throwthrow we;
}
}
catch (Exception ex)
{
LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest Exception: " + ex.Message);
}
LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest Succeeded.");
}
public TesterRequest(string p_strURL)
{ {
try
{
TesterCommThreadInfo tcti = new TesterCommThreadInfo();
tcti.m_strURL = p_strURL;
LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest: Begin queueing a thread to trigger program download.");
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), tcti);
// Service.LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest: Begin - Create and launch a new thread to trigger program download.");
// 6/4/18 changed to simple thread from thread pool for better reliability.
//Thread tr = new Thread(() => ThreadProc(tcti));
//tr.Start();
//Service.LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest: End - Create and launch a new thread to trigger program download.");
LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest: End queued a thread to trigger program download.");
}
catch (ThreadAbortException tae)
{
Service.LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest Thread Abort Exception Creating and/or launching new thread for program download: " + tae.Message);
}
catch (AppDomainUnloadedException aue)
{
Service.LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest App Domain Unloaded Exception Creating and/or launching new thread for program download: " + aue.Message);
}
catch (Exception ex)
{
Service.LogMessage(DateTime.Now.ToString() + " ms: " + DateTime.Now.Millisecond.ToString() + ". " + "TesterRequest Exception Creating and/or launching new thread for program download: " + ex.Message);
}
}
}
}