public static async Task <IEnumerable <Message> > ReadMessagesFromMessageBoxAsync(Uri uri)
 {
     if (uri == null)
     {
         throw new ArgumentException($"The {nameof(uri)} parameter cannot be null.", nameof(uri));
     }
     for (int k = 1; k <= ConfigurationHelper.MaxQueryRetryCount; k++)
     {
         try
         {
             IMessageBoxService messageBoxService =
                 ServiceProxy.Create <IMessageBoxService>(
                     ConfigurationHelper.MessageBoxServiceUri,
                     new ServicePartitionKey(PartitionResolver.Resolve(uri.AbsoluteUri, ConfigurationHelper.MessageBoxServicePartitionCount)));
             return(await messageBoxService.ReadMessagesAsync(uri));
         }
         catch (FabricTransientException ex)
         {
             ActorEventSource.Current.Error(ex);
         }
         catch (AggregateException ex)
         {
             foreach (Exception innerException in ex.InnerExceptions)
             {
                 ActorEventSource.Current.Error(innerException);
             }
         }
         catch (Exception ex)
         {
             ActorEventSource.Current.Error(ex);
         }
         await Task.Delay(ConfigurationHelper.BackoffQueryDelay);
     }
     throw new TimeoutException(Constants.RetryTimeoutExhausted);
 }
        public async Task <IEnumerable <Message> > ReadMessagesAsync(GatewayRequest request)
        {
            try
            {
                // Validates parameter
                if (request.ObserverEntityId == null)
                {
                    throw new ArgumentException($"Parameter {nameof(request)} is null or invalid.", nameof(request));
                }

                // Gets service proxy
                IMessageBoxService proxy = GetServiceProxy(
                    PartitionResolver.Resolve(
                        request.ObserverEntityId.EntityUri.AbsoluteUri,
                        OwinCommunicationListener.MessageBoxServicePartitionCount),
                    OwinCommunicationListener.MessageBoxServiceUri);
                if (proxy == null)
                {
                    throw new ApplicationException("The ServiceProxy cannot be null.");
                }

                // Invokes actor using proxy
                ServiceEventSource.Current.Message($"Reading messages from the MessageBox...\r\n[Observer]: {request.ObserverEntityId}");
                return(await proxy.ReadMessagesAsync(request.ObserverEntityId.EntityUri));
            }
            catch (AggregateException ex)
            {
                if (!(ex.InnerExceptions?.Count > 0))
                {
                    throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
                }
                foreach (Exception exception in ex.InnerExceptions)
                {
                    ServiceEventSource.Current.Message(exception.Message);
                }
                throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.Message(ex.Message);
                throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
            }
        }