/// <summary> /// Schedules a callback at a specified time. /// </summary> /// <param name="serviceBaseUri">A <see cref="T:System.Uri"/> representing the scheme, host, and port for the Revalee service (example: http://localhost:46200).</param> /// <param name="callbackTime">A <see cref="T:System.DateTimeOffset"/> that represents the scheduled moment of the callback.</param> /// <param name="callbackUri">An absolute <see cref="T:System.Uri"/> that will be requested on the callback.</param> /// <returns>A <see cref="T:System.Guid"/> that serves as an identifier for the successfully scheduled callback.</returns> /// <exception cref="T:System.ArgumentNullException"><paramref name="serviceBaseUri" /> is null.</exception> /// <exception cref="T:System.ArgumentNullException"><paramref name="callbackUri" /> is null.</exception> /// <exception cref="T:Revalee.Client.RevaleeRequestException">The callback request failed during communication with the Revalee service.</exception> public static Guid ScheduleCallback(Uri serviceBaseUri, DateTimeOffset callbackTime, Uri callbackUri) { if (serviceBaseUri == null) { throw new ArgumentNullException("serviceBaseUri"); } if (callbackUri == null) { throw new ArgumentNullException("callbackUri"); } if (!callbackUri.IsAbsoluteUri) { callbackUri = ToAbsoluteUri(callbackUri); } string requestUrl = BuildScheduleRequestUrl(serviceBaseUri, callbackTime.UtcDateTime, callbackUri); WebRequest webRequest = CreateRequest(requestUrl); string authorizationHeaderValue = RequestValidator.Issue(callbackUri); if (!string.IsNullOrEmpty(authorizationHeaderValue)) { webRequest.Headers.Add(_RevaleeAuthHttpHeaderName, authorizationHeaderValue); } Guid trackingGuid = ProcessScheduledCallbackSync(webRequest, serviceBaseUri, callbackUri); if (Guid.Empty.Equals(trackingGuid)) { throw new RevaleeRequestException(serviceBaseUri, callbackUri, new InvalidOperationException("Service did not return a valid tracking number.")); } return(trackingGuid); }
public async static Task <Guid> RequestCallbackAsync(Uri callbackUri, DateTimeOffset callbackTime, CancellationToken cancellationToken) { if (callbackUri == null) { throw new ArgumentNullException("callbackUri"); } if (!callbackUri.IsAbsoluteUri) { throw new ArgumentException("Callback Uri is not an absolute Uri.", "callbackUri"); } var serviceBaseUri = new ServiceBaseUri(); try { bool isDisposalRequired; HttpClient httpClient = AcquireHttpClient(out isDisposalRequired); try { Uri requestUri = BuildScheduleRequestUri(serviceBaseUri, callbackTime.UtcDateTime, callbackUri); string authorizationHeaderValue = RequestValidator.Issue(callbackUri); using (var requestMessage = new HttpRequestMessage(HttpMethod.Put, requestUri)) { if (!string.IsNullOrEmpty(authorizationHeaderValue)) { requestMessage.Headers.Add(_RevaleeAuthHttpHeaderName, authorizationHeaderValue); } using (HttpResponseMessage response = await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false)) { if (response.IsSuccessStatusCode) { string responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return(Guid.ParseExact(responseText, "D")); } else { throw new RevaleeRequestException(serviceBaseUri, callbackUri, new WebException(string.Format("The remote server returned an error: ({0}) {1}.", (int)response.StatusCode, response.ReasonPhrase), WebExceptionStatus.ProtocolError)); } } } } finally { if (isDisposalRequired) { httpClient.Dispose(); } } } catch (AggregateException aex) { throw new RevaleeRequestException(serviceBaseUri, callbackUri, aex.Flatten().InnerException); } catch (WebException wex) { throw new RevaleeRequestException(serviceBaseUri, callbackUri, wex); } }
/// <summary> /// Schedules a callback at a specified time asynchronously. /// </summary> /// <param name="serviceBaseUri">A <see cref="T:System.Uri"/> representing the scheme, host, and port for the Revalee service (example: http://localhost:46200).</param> /// <param name="callbackTime">A <see cref="T:System.DateTimeOffset"/> that represents the scheduled moment of the callback.</param> /// <param name="callbackUri">An absolute <see cref="T:System.Uri"/> that will be requested on the callback.</param> /// <returns>A System.Threading.Tasks.Task<System.Guid> and when complete, the result will be an identifier for the successfully scheduled callback.</returns> /// <exception cref="T:System.ArgumentNullException"><paramref name="serviceBaseUri" /> is null.</exception> /// <exception cref="T:System.ArgumentNullException"><paramref name="callbackUri" /> is null.</exception> /// <exception cref="T:Revalee.Client.RevaleeRequestException">The callback request failed during communication with the Revalee service.</exception> public static Task <Guid> ScheduleCallbackAsync(Uri serviceBaseUri, DateTimeOffset callbackTime, Uri callbackUri) { if (serviceBaseUri == null) { throw new ArgumentNullException("serviceBaseUri"); } if (callbackUri == null) { throw new ArgumentNullException("callbackUri"); } if (!callbackUri.IsAbsoluteUri) { callbackUri = ToAbsoluteUri(callbackUri); } string requestUrl = BuildScheduleRequestUrl(serviceBaseUri, callbackTime.UtcDateTime, callbackUri); WebRequest webRequest = CreateRequest(requestUrl); string authorizationHeaderValue = RequestValidator.Issue(callbackUri); if (!string.IsNullOrEmpty(authorizationHeaderValue)) { webRequest.Headers.Add(_RevaleeAuthHttpHeaderName, authorizationHeaderValue); } Task <WebResponse> responseTask = Task.Factory.FromAsync <WebResponse>(webRequest.BeginGetResponse, webRequest.EndGetResponse, webRequest); ThreadPool.RegisterWaitForSingleObject((responseTask as IAsyncResult).AsyncWaitHandle, TimeoutCallback, webRequest, webRequest.Timeout, true); return(responseTask.ContinueWith(task => { HttpWebResponse response = null; try { response = (HttpWebResponse)task.Result; } catch (AggregateException aex) { throw new RevaleeRequestException(serviceBaseUri, callbackUri, aex.Flatten().InnerException); } catch (WebException wex) { throw new RevaleeRequestException(serviceBaseUri, callbackUri, wex); } try { if (response.StatusCode == HttpStatusCode.OK) { using (var reader = new StreamReader(response.GetResponseStream())) { string responseText = reader.ReadToEnd(); return Guid.ParseExact(responseText, "D"); } } return Guid.Empty; } catch (WebException wex) { throw new RevaleeRequestException(serviceBaseUri, callbackUri, wex); } finally { if (response != null) { response.Close(); } } })); }