private TResponse ExecServerRequest <TResponse, TRequest>(TRequest request, SvcGatewayDelegate <TRequest, TResponse> svcGatewayMethod, TimeSpan timeout, AsyncResults asyncResults) where TResponse : ServiceResponse where TRequest : ICommonRequest { TResponse response = svcGatewayMethod(request); if (response.ResponseCode != ResponseCode.Succeeded) { return(response); } object serviceResponse; if (asyncResults.TryTake(out serviceResponse, timeout)) { if (serviceResponse is IFaulted && ((IFaulted)serviceResponse).IsFaulted) { throw new AggregateException(((IFaulted)serviceResponse).Message); } response = (TResponse)serviceResponse; if (response != null && response.ResponseCode != ResponseCode.Succeeded && !string.IsNullOrWhiteSpace(response.ErrorMessage)) { throw new ServerException(response.ErrorMessage); } return(response); } string timeOutString = TimeSpan.FromMinutes(2) > timeout ? timeout.TotalSeconds + "seconds" : "2 minutes"; throw new TimeoutException("We did not receive of reply from the server after " + timeOutString + " for transaction " + asyncResults.Id); }
/// <summary> /// Provides a synchronous wait version of a server request and callback. /// If the caller thread is UI thread, it executes asynchronously. /// If the caller thread is not UI thread, it blocks the caller thread. /// </summary> /// <typeparam name="T">The type expected via callback.</typeparam> /// <typeparam name="TResponse"></typeparam> /// <typeparam name="TRequest"></typeparam> /// <param name="request"></param> /// <param name="svcGatewayMethod"></param> /// <param name="timeout">The time to wait before giving up on the service response.</param> /// <returns></returns> /// <remarks>Note that this does not currently support progress reporting. If the service provides progress /// reports this will need to be amended.</remarks> protected TResponse ServerRequest <TResponse, TRequest>(TRequest request, SvcGatewayDelegate <TRequest, TResponse> svcGatewayMethod, TimeSpan timeout) where TResponse : ServiceResponse where TRequest : ICommonRequest { AsyncResults asyncResults = AddToResponseQueue(request.RequestID); TimeSpan defaultTimeout = TimeSpan.FromMinutes(2); if (defaultTimeout < timeout) { timeout = defaultTimeout; } if (UIThreadSingleton.IsCreated && UIThreadSingleton.IsCallFromUIThread) { Task <TResponse> callCompleted = TaskFactoryHelper <TResponse> .StartNew( () => ExecServerRequest(request, svcGatewayMethod, timeout, asyncResults), BackgroundTaskScheduler.OnBackground()); while (!callCompleted.IsFinishedRunning()) { Application.DoEvents(); } if (callCompleted.Status != TaskStatus.RanToCompletion) { if (callCompleted.Exception != null) { AggregateException aggregateException = callCompleted.Exception.Flatten(); if (aggregateException.InnerException != null) { throw aggregateException.InnerException; } } } return(callCompleted.Result); } else { return(ExecServerRequest(request, svcGatewayMethod, timeout, asyncResults)); } }
/// <summary> /// Provides a synchronous wait version of a server request and callback. /// If the caller thread is UI thread, it executes asynchronously. /// If the caller thread is not UI thread, it blocks the caller thread. /// By Default it times out after 2 minutes. /// </summary> /// <typeparam name="TResponse"></typeparam> /// <typeparam name="TRequest"></typeparam> /// <param name="request"></param> /// <param name="svcGatewayMethod"></param> /// <returns></returns> protected TResponse ServerRequest <TResponse, TRequest>(TRequest request, SvcGatewayDelegate <TRequest, TResponse> svcGatewayMethod) where TResponse : ServiceResponse where TRequest : ICommonRequest { return(ServerRequest(request, svcGatewayMethod, TimeSpan.FromMinutes(2))); }