Esempio n. 1
0
 public async Task RetryExecAsyncTest()
 {
     try
     {
         await PolicyHelper.RetryExecAsync <DivideByZeroException>(async() =>
         {
             Console.WriteLine("func.........");
             throw new DivideByZeroException();
         });
     }
     catch (Exception ex)
     {
     }
     Assert.Pass();
 }
Esempio n. 2
0
        public static async Task <string> getTokenAsync(string strURL, string strContent, string mediaType, int RetryCount = 3)
        {
            string strToken = string.Empty;
            await limitLock.WaitAsync().ConfigureAwait(false);

            try
            {
                using (var client = new HttpClient())
                {
                    using (var content = new StringContent(strContent, Encoding.UTF8, mediaType))
                    {
                        await PolicyHelper.RetryExecAsync <TokenException>(async() =>
                        {
                            try
                            {
                                var response = await client.PostAsync(strURL, content);
                                response.EnsureSuccessStatusCode();
                                var responseStr = await response.Content.ReadAsStringAsync();
                                if (responseStr.Contains("access_token"))
                                {
                                    var temp = JsonConvert.DeserializeObject <dynamic>(responseStr);
                                    strToken = temp.access_token;
                                }
                                else
                                {
                                    throw new TokenException();
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new TokenException(ex.Message, ex);
                            }
                        });
                    }
                }
            }
            catch (TokenException ex)
            {
                Console.WriteLine($"Excepiton:{ex.Message}");
            }
            finally
            {
                limitLock.Release();
            }
            return(strToken);
        }
Esempio n. 3
0
        public async Task <string> SendRequest(HttpClient httpClient, string strURL, Dictionary <string, string> queryParam, int RetryCount = 3)
        {
            string result = string.Empty;

            try
            {
                await PolicyHelper.RetryExecAsync <TokenException>(async() =>
                {
                    result = await SendRequest(httpClient, strURL, queryParam);
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Excepiton:{ex.Message}");
            }
            return(result);
        }