Results 1 to 1 of 1

Thread: TaskFactory WebRequests and DataBase Writing

Threaded View

  1. #1

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Question TaskFactory WebRequests and DataBase Writing

    Need help or at least check what I'm doing wrong.

    I have a windows service that runs everyday at a schedule time, it takes a list of internal IDs and a related URL, after dividing the list into batches (i'm using batches with 2 elemnts), the code loop through all batches, and make web requests and write the result to the database (log) for each id in the current batch. Each request has is own thread and a predefined/static timeout.

    I'm having two problems:

    First one, the log, are written but the first time it runs it kind duplicate the first Id that it founds in the list, sample at the bottom.

    Second one, this one, i don't know if it's "my" problem, or the website, it's always the same URL, only different QS parameteres. I have a static timeout for each request, in the sample 3 seconds, after the second request I always get timeout.

    c# Code:
    1. List<List<KeyValuePair<long, string>>> batchList = new List<List<KeyValuePair<long, string>>>(10);
    2.  
    3. //Get Credentials and timeout value one time
    4. ICredentials credentials = GetCredentials(); //Get default credentials, doesnt change
    5. int clickTimeout = 3; //will be seconds
    6. int j = 1;
    7.  
    8. //Loop
    9. foreach (List<KeyValuePair<long, string>> batch in batchList)
    10. {
    11.     LogHelper.InsertJobLog("ServiceName", string.Format("Executing batch: {0}.", (j).ToString()), reference);
    12.     HandleProcessBatch(batch, credentials, clickTimeout);
    13.     LogHelper.InsertJobLog("ServiceName", string.Format("End Executing batch: {0}.", (j).ToString()), reference);
    14.     j++;
    15. }
    16.  
    17. // ....
    18.  
    19. //Handle batches of processes
    20. private static void HandleProcessBatch(List<KeyValuePair<long, string>> procList, ICredentials credentials, int clickTimeout)
    21. {
    22.     try
    23.     {
    24.         //Task list
    25.         List<Task> listaTarefas = new List<Task>();
    26.         foreach (KeyValuePair<long, string> proc in procList)
    27.         {
    28.             LogHelper.InsertJobLog("ServiceName", string.Format("ProcID {0} will be added to the execution list: {1}", proc.Key, proc.Value), null);
    29.             listaTarefas.Add(
    30.                 Task.Factory.StartNew(
    31.                     () => SimulateURLRequest(proc.Key,proc.Value, credentials, clickTimeout)
    32.                     )
    33.                 );
    34.         }
    35.  
    36.         //Wait for all tasks in this list to end
    37.         Task.WaitAll(listaTarefas.ToArray());
    38.     }
    39.     catch (Exception ex)
    40.     {
    41.         EventLogWrite.Error("ServiceName-HandleProcessBatch", ex.Message);
    42.     }
    43.  
    44. }
    45.  
    46. //Called in diferrent threads
    47. private static void SimulateURLRequest(long processID, string processURL, ICredentials credentials, int timeout)
    48. {
    49.     try
    50.     {
    51.         Uri ipacURL;
    52.         if (!Uri.TryCreate(processURL, UriKind.Absolute, out ipacURL) || (ipacURL.Scheme != Uri.UriSchemeHttp && ipacURL.Scheme != Uri.UriSchemeHttps))
    53.         {
    54.             LogHelper.InsertJobLog("ServiceName", string.Format("Proc {0} URL not valid: {1}", processID.ToString(), processURL),null);
    55.             return;
    56.         }
    57.         WebRequest wr = WebRequest.Create(ipacURL);
    58.         wr.Credentials = credentials;
    59.         wr.Timeout = (timeout * 1000);
    60.         HttpWebResponse answer = (HttpWebResponse)wr.GetResponse();
    61.  
    62.         LogHelper.InsertJobLog("ServiceName", string.Format("Proc {0} result: {1}", processID.ToString(), Enum.GetName(typeof(HttpStatusCode), answer.StatusCode)),null);
    63.     }
    64.     catch (WebException wex)
    65.     {
    66.         LogHelper.InsertJobLog("ServiceName", wex.Message,null);          
    67.     }
    68.     catch (Exception ex)
    69.     {
    70.         LogHelper.InsertJobLog("ServiceName", ex.Message, null);
    71.         EventLogWrite.Error("ServiceName-SimulateURLRequest", ex.Message);
    72.     }
    73. }

    Logs sample:
    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.
    The ID 20973792 was written two times to the database, and after that it starts to yield timeouts...

    I don't see what I'm doing wrong...
    Last edited by mickey_pt; Nov 20th, 2019 at 12:18 PM.

    Rate People That Helped You
    Mark Thread Resolved When Resolved

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width