/// <summary> /// Asynchronously tries to perform the refresh request. /// </summary> /// <param name="security">The security.</param> /// <param name="conversationId">The conversation identifier.</param> /// <returns>The SOAP result of the Ping response.</returns> public async Task <SoapResult <OTA_PingRS> > TryRefreshAsync(Security security, string conversationId) { OTA_PingRQ request = this.soapServiceFactory.CreatePingRequest(); SWSService service = this.soapServiceFactory.CreatePingService(conversationId, security); var source = new TaskCompletionSource <SoapResult <OTA_PingRS> >(); service.OTA_PingRQCompleted += (s, e) => { if (SoapHelper.HandleErrors(e, source)) { if (e.Result.Items.Any()) { ErrorsType error = e.Result.Items[0] as ErrorsType; if (error != null && error.Error != null && error.Error.Length > 0) { source.TrySetResult(SoapResult <OTA_PingRS> .Error(error.Error.First())); return; } } source.TrySetResult(SoapResult <OTA_PingRS> .Success(e.Result)); } }; service.OTA_PingRQAsync(request); return(await source.Task); }
/// <summary> /// Handles the errors in asynchronous calls. /// If an error has occurred, sets a result in the task completion source containing information about the problem: /// either the Exception that has occurred or the cancellation flag. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="args">The <see cref="AsyncCompletedEventArgs"/> instance containing the event data.</param> /// <param name="source">The <see cref="TaskCompletionSource"/> instance related to the SOAP result.</param> /// <returns><c>true</c> if no errors have occurred; otherwise <c>false</c></returns> public static bool HandleErrors <TResult>(AsyncCompletedEventArgs args, TaskCompletionSource <SoapResult <TResult> > source) { if (args.Error != null) { source.TrySetResult(SoapResult <TResult> .Error(args.Error)); return(false); } else if (args.Cancelled) { source.TrySetResult(SoapResult <TResult> .Cancelled()); return(false); } return(true); }
/// <summary> /// Creates a new session asynchronously. /// </summary> /// <param name="conversationId">The conversation identifier.</param> /// <returns>The SOAP result containing the SessionCreate response.</returns> public async Task <SoapResult <SessionCreateRS> > CreateSessionAsync(string conversationId) { SessionCreateRQ request = this.soapServiceFactory.CreateSessionCreateRequest(); SessionCreateRQService service = this.soapServiceFactory.CreateSessionCreateService(conversationId); // This is an asynchronous SOAP call. // We need to convert event-based asynchrous call to task-based one. // The event is triggered when the action is completed. // Then it sets the result and ends the task. var source = new TaskCompletionSource <SoapResult <SessionCreateRS> >(); service.SessionCreateRQCompleted += (s, e) => { if (SoapHelper.HandleErrors(e, source)) { source.TrySetResult(SoapResult <SessionCreateRS> .Success(e.Result, service.SecurityValue)); } }; service.SessionCreateRQAsync(request); // Return the asynchronous task. return(await source.Task); }