需要帮助或至少检查我做错了什么。
我有一个Windows服务,该服务每天按计划运行,它需要一个内部ID和相关URL的列表,然后将该列表分成若干批(我使用的是带有2个元素的批处理),代码循环遍历所有批处理,并发出Web请求,并将当前批处理中每个ID的结果写入数据库(日志)。每个请求都有自己的线程和预定义/静态超时。
我遇到两个问题:
第一个日志被写入,但是第一次运行时,它会与它在列表中找到的第一个ID(底部的示例)相同。
第二个,这个,我不知道这是“我的”问题,还是网站,它始终是相同的URL,只是不同的QS参数。我对每个请求都有一个静态超时,在示例3秒内,在第二个请求之后,我总是超时。
List<List<keyvaluepair<long,string>>> batchList = new List<List<keyvaluepair<long,string>>>(10);
//Get Credentials and timeout value one time
ICredentials credentials = getcredentials(); //Get default credentials,doesnt change
int clickTimeout = 3; //will be seconds
int j = 1;
//Loop
foreach (List<keyvaluepair<long,string>> batch in batchList)
{
LogHelper.InsertJobLog("ServiceName",string.Format("Executing batch: {0}.",(j).ToString()),reference);
HandleProcessBatch(batch,credentials,clickTimeout);
LogHelper.InsertJobLog("ServiceName",string.Format("End Executing batch: {0}.",reference);
j++;
}
// ....
//Handle batches of processes
private static void HandleProcessBatch(List<keyvaluepair<long,string>> procList,ICredentials credentials,int clickTimeout)
{
try
{
//Task list
List<Task> listaTarefas = new List<Task>();
foreach (keyvaluepair<long,string> proc in procList)
{
LogHelper.InsertJobLog("ServiceName",string.Format("ProcID {0} will be added to the execution list: {1}",proc.Key,proc.Value),null);
listaTarefas.Add(
Task.Factory.StartNew(
() => SimulateURLRequest(proc.Key,proc.Value,clickTimeout)
)
);
}
//Wait for all tasks in this list to end
Task.WaitAll(listaTarefas.ToArray());
}
catch (Exception ex)
{
EventLogWrite.Error("ServiceName-HandleProcessBatch",ex.Message);
}
}
//Called in diferrent threads
private static void SimulateURLRequest(long processID,string processURL,int timeout)
{
try
{
Uri ipacURL;
if (!Uri.TryCreate(processURL,UriKind.Absolute,out ipacURL) || (ipacURL.Scheme != Uri.UriSchemeHttp && ipacURL.Scheme != Uri.UriSchemeHttps))
{
LogHelper.InsertJobLog("ServiceName",string.Format("Proc {0} URL not valid: {1}",processID.ToString(),processURL),null);
return;
}
WebRequest wr = WebRequest.Create(ipacURL);
wr.Credentials = credentials;
wr.Timeout = (timeout * 1000);
Httpwebresponse answer = (Httpwebresponse)wr.GetResponse();
LogHelper.InsertJobLog("ServiceName",string.Format("Proc {0} result: {1}",Enum.GetName(typeof(HttpStatusCode),answer.StatusCode)),null);
}
catch (WebException wex)
{
LogHelper.InsertJobLog("ServiceName",wex.Message,null);
}
catch (Exception ex)
{
LogHelper.InsertJobLog("ServiceName",ex.Message,null);
EventLogWrite.Error("ServiceName-SimulateURLRequest",ex.Message);
}
}
日志示例:
ServiceName 2019-11-11 20:01:03.000 Daily Task Ended.
ServiceName 2019-11-11 20:01:02.000 End Executing batch: 2.
ServiceName 2019-11-11 20:01:02.000 The operation has timed out
ServiceName 2019-11-11 20:00:59.000 ProcID 20973800 will be added the the execution list: URL_20973800
ServiceName 2019-11-11 20:00:59.000 Executing batch: 2.
ServiceName 2019-11-11 20:00:59.000 End Executing batch: 1.
ServiceName 2019-11-11 20:00:59.000 Proc 20973792 result: OK
ServiceName 2019-11-11 20:00:59.000 Proc 20973792 result: OK
ServiceName 2019-11-11 20:00:58.000 ProcID 20973792 will be added the the execution list: URL_20973792
ServiceName 2019-11-11 20:00:58.000 ProcID 20972361 will be added the the execution list: URL_20972361
ServiceName 2019-11-11 20:00:58.000 Executing batch: 1.
ServiceName 2019-11-11 20:00:34.000 Daily Task Started.
ID 20973792被两次写入数据库,此后它开始产生超时...
我看不到我在做什么...