Exemplo n.º 1
0
        public async static Task <CosmosAccountSettings> GetDatabaseAccountFromAnyLocationsAsync(
            Uri defaultEndpoint, IList <string> locations, Func <Uri, Task <CosmosAccountSettings> > getDatabaseAccountFn)
        {
            try
            {
                CosmosAccountSettings databaseAccount = await getDatabaseAccountFn(defaultEndpoint);

                return(databaseAccount);
            }
            catch (Exception e)
            {
                DefaultTrace.TraceInformation("Fail to reach global gateway {0}, {1}", defaultEndpoint, e.ToString());
                if (locations.Count == 0)
                {
                    throw;
                }
            }

            for (int index = 0; index < locations.Count; index++)
            {
                try
                {
                    CosmosAccountSettings databaseAccount = await getDatabaseAccountFn(LocationHelper.GetLocationEndpoint(defaultEndpoint, locations[index]));

                    return(databaseAccount);
                }
                catch (Exception e)
                {
                    DefaultTrace.TraceInformation("Fail to reach location {0}, {1}", locations[index], e.ToString());
                    if (index == locations.Count - 1)
                    {
                        // if is the last one, throw exception
                        throw;
                    }
                }
            }

            // we should never reach here. Make compiler happy
            throw new Exception();
        }
        public static async Task <AccountProperties> GetDatabaseAccountFromAnyLocationsAsync(
            Uri defaultEndpoint, IList <string> locations, Func <Uri, Task <AccountProperties> > getDatabaseAccountFn)
        {
            ExceptionDispatchInfo capturedException = null;

            try
            {
                AccountProperties databaseAccount = await getDatabaseAccountFn(defaultEndpoint);

                return(databaseAccount);
            }
            catch (Exception e)
            {
                DefaultTrace.TraceInformation("Fail to reach global gateway {0}, {1}", defaultEndpoint, e.ToString());

                if (IsNonRetriableException(e))
                {
                    DefaultTrace.TraceInformation("Exception is not retriable");
                    throw;
                }

                if (locations.Count == 0)
                {
                    throw;
                }

                // Save the exception and rethrow it at the end after trying all regions
                capturedException = ExceptionDispatchInfo.Capture(e);
            }

            for (int index = 0; index < locations.Count; index++)
            {
                try
                {
                    AccountProperties databaseAccount = await getDatabaseAccountFn(LocationHelper.GetLocationEndpoint(defaultEndpoint, locations[index]));

                    return(databaseAccount);
                }
                catch (Exception e)
                {
                    DefaultTrace.TraceInformation("Fail to reach location {0}, {1}", locations[index], e.ToString());

                    // if is the last region, throw exception
                    if (index == locations.Count - 1)
                    {
                        // The reason for rethrowing the first exception is that the locations list might contain invalid regions,
                        // so the last exception would be some generic exception ("The remote name could not be resolved") instead of the real exception.
                        // Location list containing invalid regions is quite common when SetCurrentLocation is used since it will add all Azure regions to the list
                        if (capturedException != null)
                        {
                            capturedException.Throw();
                        }

                        throw;
                    }
                }
            }

            // we should never reach here. Make compiler happy
            throw new Exception();
        }
 private Task TryGetAccountPropertiesFromRegionalEndpointsAsync(string location)
 {
     return(this.GetAndUpdateAccountPropertiesAsync(
                LocationHelper.GetLocationEndpoint(this.DefaultEndpoint, location)));
 }