/// <summary> /// Called for first web request. /// </summary> /// <param name="requestParameter">Could be modified for each subsequent request</param> /// <param name="webRequestComplete"></param> /// <param name="webRequestException"></param> /// <param name="webRequestStart"></param> /// <param name="webRequestProgress"></param> /// <param name="priority"></param> private RequestState DoQueueRequest(RequestParameter requestParameter, RequestStartCallback webRequestStart, RequestCompleteCallback webRequestComplete, RequestExceptionCallback webRequestException, RequestProgressCallback webRequestProgress, int priority) { if (requestParameter == null) { throw new ArgumentNullException("requestParameter"); } if (_queuedRequests.Contains(requestParameter.RequestUri.CanonicalizedUri())) { return(null); // httpRequest already there } _queuedRequests.Add(requestParameter.RequestUri.CanonicalizedUri(), null); var webRequest = PrepareRequest(requestParameter); RequestState state = new RequestState(webRequest, priority, requestParameter); state.WebRequestBeforeStart += webRequestStart; state.WebRequestCompleted += webRequestComplete; state.WebRequestException += webRequestException; state.WebRequestProgress += webRequestProgress; PerformHttpRequestAsync(state, priority); return(state); }
/// <summary> /// Used to make GET requests for multiple URLs asynchronously /// </summary> /// <param name="requests">The URLs to be fetched</param> /// <param name="webRequestStart">callback invoked when each GET request starts</param> /// <param name="webRequestComplete">callback invoked when each GET request completes</param> /// <param name="webRequestException">callback invoked when each GET request fails</param> /// <exception cref="NotSupportedException">The request scheme specified in address has not been registered.</exception> /// <exception cref="ArgumentNullException">The requestParameter is a null reference</exception> public void QueueRequestsAsync(List <RequestParameter> requests, RequestStartCallback webRequestStart, RequestCompleteCallback webRequestComplete, RequestExceptionCallback webRequestException) { const int priority = 10; // needed for additional requests // Parallel options object specifies scheduler and max concurrent threads ParallelOptions options = new ParallelOptions() { TaskScheduler = _scheduler, MaxDegreeOfParallelism = _scheduler.MaximumConcurrencyLevel }; // Create an instance of the RequestState and perform the HTTP request for each of the request parameters Parallel.ForEach(requests, options, request => { if (request != null && !_queuedRequests.Contains(request.RequestUri.CanonicalizedUri())) { _queuedRequests.Add(request.RequestUri.CanonicalizedUri(), null); var webRequest = PrepareRequest(request); var state = new RequestState(webRequest, priority, request); state.WebRequestBeforeStart += webRequestStart; state.WebRequestCompleted += webRequestComplete; state.WebRequestException += webRequestException; PerformHttpRequest(state); } }); }
/// <summary> /// Used to a queue an HTTP request for processing /// </summary> /// <param name="requestParameter"></param> /// <param name="webRequestComplete"></param> /// <param name="webRequestException"></param> /// <param name="webRequestStart"></param> /// <param name="priority"></param> /// <exception cref="NotSupportedException">The request scheme specified in address has not been registered.</exception> /// <exception cref="ArgumentNullException">The requestParameter is a null reference</exception> /// <exception cref="System.Security.SecurityException">The caller does not have permission to connect to the requested URI or a URI that the request is redirected to.</exception> internal RequestState QueueRequest(RequestParameter requestParameter, RequestStartCallback webRequestStart, RequestCompleteCallback webRequestComplete, RequestExceptionCallback webRequestException, int priority) { return (DoQueueRequest(requestParameter, webRequestStart, webRequestComplete, webRequestException, null, priority)); }