Ejemplo n.º 1
0
        private HttpClient CreateHttpClient()
        {
            HttpClient client = new HttpClient();

            client.Timeout = ConfigurationAccessor.HttpTimeout;

            foreach (KeyValuePair <string, string> eachKVP in ConfigurationAccessor.GetCustomHeaders())
            {
                Trace.TraceInformation("Adding custom header {0}: {1}",
                                       eachKVP.Key,
                                       eachKVP.Value);
                bool result = client.DefaultRequestHeaders.TryAddWithoutValidation(eachKVP.Key, eachKVP.Value);
                if (!result)
                {
                    Trace.TraceWarning("Cannot add custom header {0}: {1}",
                                       eachKVP.Key,
                                       eachKVP.Value);
                }
            }

            return(client);
        }
Ejemplo n.º 2
0
        public void Run(string[] args)
        {
            ServicePointManager.ServerCertificateValidationCallback += this.ServicePointManager_ServerCertificateValidationCallback;
            TimeSpan   interval = ConfigurationAccessor.IntervalTimeSpan;
            HttpClient client   = clientFactory.Value;

            while (!this.cts.Token.IsCancellationRequested)
            {
                this.mres.Wait(this.cts.Token);
                IEnumerable <SiteListItem> uriList = ConfigurationAccessor.GetSiteListFromSiteListFilePath();

                int count = uriList.Count();
                if (count < 1)
                {
                    Trace.TraceWarning("No URL list specified. Please check your list file.");
                }
                else
                {
                    Trace.TraceInformation("Invoking {0} URIs.", count);
                }

                List <Task> taskList = new List <Task>();
                foreach (SiteListItem eachItem in uriList)
                {
                    Trace.TraceInformation(eachItem.ToString());
                    HttpRequestMessage requestMessage = new HttpRequestMessage(
                        eachItem.Method,
                        eachItem.Uri);
                    Task t = client.SendAsync(requestMessage, this.cts.Token)
                             .ContinueWith(x =>
                    {
                        switch (x.Status)
                        {
                        case TaskStatus.Canceled:
                            Trace.TraceWarning("Task has been cancelled.");
                            break;

                        case TaskStatus.Faulted:
                            Trace.TraceError("Task has an error/exception. {0}", x.Exception);
                            break;

                        case TaskStatus.RanToCompletion:
                            if (x.Result.IsSuccessStatusCode)
                            {
                                Trace.TraceInformation("Task has been done with {0} {1}", (int)x.Result.StatusCode, x.Result.ReasonPhrase);
                            }
                            else
                            {
                                Trace.TraceWarning("Task has been done with {0} {1}", (int)x.Result.StatusCode, x.Result.ReasonPhrase);
                            }
                            break;

                        default:
                            Trace.TraceWarning("Unexpected task status occurred. {0}", x.Status);
                            break;
                        }
                    });
                    taskList.Add(t);
                }
                Trace.TraceInformation("Waiting remained tasks.");
                if (!Task.WaitAll(taskList.ToArray(), (int)ConfigurationAccessor.WaitTimeout.TotalMilliseconds, this.cts.Token))
                {
                    Trace.TraceWarning("Waiting timeout occurred.");
                }

                Trace.TraceInformation("Perform sleep - {0}", interval);
                this.cts.Token.WaitHandle.WaitOne(interval);
            }
        }