예제 #1
0
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <typeparam name="T">The type.</typeparam>
        /// <param name="query">The query.</param>
        /// <returns>A value</returns>
        public static T GetValue <T>(DataServiceQuerySingle <T> query)
        {
            var tries    = 0;
            var maxTries = 3;

            var watch = new Stopwatch();

            watch.Start();

            while (tries < maxTries)
            {
                try
                {
                    var result = query.GetValueAsync().Result;
                    watch.Stop();
                    LogInfo($"     <.><.> GetValue<T>: {query.RequestUri}: {watch.ElapsedMilliseconds}ms");

                    return(result);
                }
                catch (DataServiceQueryException ex)
                {
                    LogError($"Exception {ex.InnerException.Message} on GetValue:{ex.Response.Query}", typeof(Proxy));
                    return(default(T));
                }
                catch (AggregateException ex)
                {
                    foreach (var e in ex.InnerExceptions)
                    {
                        if (e is DataServiceQueryException dataserviceQueryException)
                        {
                            switch (dataserviceQueryException.Response.StatusCode)
                            {
                            case 404:
                                // If the item is not found, we return a null.
                                // That is easier to code for than throwing an exception
                                LogError($"Entity Not Found Exception (404) - Query: {dataserviceQueryException.Response.Query} - Message:{dataserviceQueryException.Message}", typeof(Proxy));
                                return(default(T));

                            default:
                                LogError($"Query Exception - Query:{dataserviceQueryException.Response.Query} - Message:{dataserviceQueryException.Message}", typeof(Proxy));
                                throw dataserviceQueryException;
                            }
                        }

                        LogError($"Exception {e.GetType()}: {e.Message} on GetValue:{query.RequestUri}", typeof(Proxy));
                        throw e;
                    }
                }
                catch (Exception ex)
                {
                    LogError($"Exception {ex.GetType()}: {ex.Message} GetValue:{query.RequestUri}", typeof(Proxy));
                    throw;
                }

                tries++;
                Thread.Sleep(100);
            }

            throw new CommerceServiceQuerySingleException(query.RequestUri.ToString());
        }
예제 #2
0
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <typeparam name="T">The type.</typeparam>
        /// <param name="query">The query.</param>
        /// <returns>A value</returns>
        public static T GetValue <T>(DataServiceQuerySingle <T> query)
        {
            var tries    = 0;
            var maxTries = 3;

            var watch = new Stopwatch();

            watch.Start();

            while (tries < maxTries)
            {
                try
                {
                    var result = query.GetValueAsync().Result;

                    watch.Stop();
                    Console.WriteLine($"     <.><.> GetValue<T>: {query.RequestUri}: {watch.ElapsedMilliseconds}ms");

                    return(result);
                }
                catch (DataServiceQueryException ex)
                {
                    WriteColoredLine(ConsoleColor.Red, $"Exception {ex.InnerException.Message} on GetValue:{ex.Response.Query}");
                }
                catch (AggregateException ex)
                {
                    WriteColoredLine(ConsoleColor.Red, $"Aggregate Exception {ex.InnerException.Message}");
                    var exception = ex.InnerException as DataServiceQueryException;
                    if (exception != null)
                    {
                        var dataserviceQueryException = exception;

                        switch (dataserviceQueryException.Response.StatusCode)
                        {
                        case 404:
                            // If the item is not found, we return a null.
                            // That is easier to code for than throwing an exception
                            WriteColoredLine(ConsoleColor.Red, $"Entity Not Found Exception (404) - Query: {dataserviceQueryException.Response.Query}");
                            return(default(T));

                        default:
                            WriteColoredLine(ConsoleColor.Red, $"Query Exception - Query:{dataserviceQueryException.Response.Query} - Message:{dataserviceQueryException.Message}");
                            throw dataserviceQueryException;
                        }
                    }
                }
                catch (Exception ex)
                {
                    WriteColoredLine(ConsoleColor.Red, $"Unknown Exception {ex.Message} GetValue:{query.RequestUri}");
                }

                tries++;
                Thread.Sleep(100);
            }

            throw new CommerceServiceQuerySingleException(query.RequestUri.ToString());
        }