/// <summary> /// The most important method of this class; makes a server call and then asynchronously waits for the response. /// </summary> /// <param name="request">The request object</param> /// <param name="onConnectionResume">Action to execute if the connection is broken, then resumed, during execution of this call.</param> /// <param name="silentMode">If true, will not display any popups or alerts for any reason.</param> /// <returns>Task result with the response</returns> public async Task <TResponse> DoRequestAsync <TRequest, TResponse>(TRequest request, Action onConnectionResume, bool silentMode = false) where TRequest : IApiRequest where TResponse : IApiResponse, new() { TResponse response = default(TResponse); try { bool cancelRequest = false; //is connection closed? if (_client.ReadyState == ReadyState.Closed || _client.ReadyState == ReadyState.Closing) { if (!(request is Aquamonix.Mobile.Lib.Domain.Requests.ConnectionRequest)) { this.FireRequestFailureEvent(request, null, RequestFailureReason.Network, onConnectionResume, (!silentMode)); cancelRequest = true; } else { _client.Dispose(); this.SetUrl(_url); } } if (!cancelRequest) { //attempt to open connection if not already if (_client.ReadyState != ReadyState.Open) { bool opened = await this.Open(request.Header.Channel, silentMode); if (!opened) { //if not a connection request, we want to fire an event if (!(request is Aquamonix.Mobile.Lib.Domain.Requests.ConnectionRequest)) { //fire the event this.FireRequestFailureEvent(request, null, RequestFailureReason.Network, onConnectionResume, !(silentMode)); } return(ResponseFactory.FabricateConnectionTimeoutResponse <TResponse>(request)); } //NEW: this 'else' clause is new else { //if not a connection request, then we want to make a connection request now if (!(request is Aquamonix.Mobile.Lib.Domain.Requests.ConnectionRequest)) { // make a request connection, if it fails it doesnt matter as it will be picked up below await ServiceContainer.UserService.RequestConnection( User.Current.Username, User.Current.Password, silentMode : true ); } } } //send the request and wait for response response = await this.Send <TResponse>(request, request.Header.Channel, silentMode); //if response is null or unsuccessful, handle if (response == null || !response.IsSuccessful) { if (response != null && response.IsReconnectResponse) { LogUtility.LogMessage("Received a Reconnect response from server; will reconnect"); this.FireRequestFailureEvent(request, response, RequestFailureReason.ServerRequestedReconnect, onConnectionResume, (!silentMode)); } else { //if response is a timeout, handle if (response != null && response.IsTimeout) { this.FireRequestFailureEvent(request, response, RequestFailureReason.Timeout, onConnectionResume, (!silentMode)); } else { if (response != null) { //if server is down, handle if (response.IsServerDownResponse) { this.FireRequestFailureEvent(request, response, RequestFailureReason.ServerDown, onConnectionResume, (!silentMode)); } else if (response.IsAuthFailure) { this.FireRequestFailureEvent(request, response, RequestFailureReason.Auth, onConnectionResume, (!silentMode)); } else { this.FireRequestFailureEvent(request, response, RequestFailureReason.Error, onConnectionResume, (!silentMode)); //show error AlertUtility.ShowAppError(response?.ErrorBody); } } else { this.FireRequestSucceededEvent(request, response); } } } } else { this.FireRequestSucceededEvent(request, response); } } } catch (Exception e) { LogUtility.LogException(e); } return(response); }