public static async Task WriteMessageToMessageBoxAsync(Uri uri, Message message)
        {
            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)));
                    await messageBoxService.WriteMessagesAsync(uri, new[] { message });

                    return;
                }
                catch (FabricTransientException ex)
                {
                    ActorEventSource.Current.Error(ex);
                    if (k == ConfigurationHelper.MaxQueryRetryCount)
                    {
                        throw;
                    }
                }
                catch (AggregateException ex)
                {
                    foreach (Exception innerException in ex.InnerExceptions)
                    {
                        ActorEventSource.Current.Error(innerException);
                    }
                    if (k == ConfigurationHelper.MaxQueryRetryCount)
                    {
                        throw;
                    }
                }
                catch (Exception ex)
                {
                    ActorEventSource.Current.Error(ex);
                    if (k == ConfigurationHelper.MaxQueryRetryCount)
                    {
                        throw;
                    }
                }
                await Task.Delay(ConfigurationHelper.BackoffQueryDelay);
            }
        }
        public async Task WriteMessagesAsync(GatewayRequest request)
        {
            try
            {
                // Validates parameter
                if ((request.ObserverEntityId == null) ||
                    (request.Messages == null) ||
                    !request.Messages.Any())
                {
                    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($"Writing messages to the MessageBox...\r\n[Observer]: {request.ObserverEntityId}");
                await proxy.WriteMessagesAsync(request.ObserverEntityId.EntityUri, request.Messages);
            }
            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));
            }
        }