private void GetToken()
        {
            using (WebClient client = new WebClient())
            {
                client.BaseAddress = this._acsBaseAddress;

                var oauthRequestValues = new NameValueCollection
                {
                    { "grant_type", GrantType },
                    { "client_id", this._clientId },
                    { "client_secret", this._clientSecret },
                    { "scope", this._scope },
                };

                RetryPolicy retryPolicy = new RetryPolicy(
                    new WebRequestTransientErrorDetectionStrategy(),
                    RetryStrategyFactory.DefaultStrategy());

                retryPolicy.ExecuteAction(
                    () =>
                {
                    byte[] responseBytes = client.UploadValues("/v2/OAuth2-13", "POST", oauthRequestValues);

                    using (var responseStream = new MemoryStream(responseBytes))
                    {
                        OAuth2TokenResponse tokenResponse = (OAuth2TokenResponse) new DataContractJsonSerializer(typeof(OAuth2TokenResponse)).ReadObject(responseStream);
                        this.AccessToken      = tokenResponse.AccessToken;
                        this._tokenExpiration = DateTime.Now.AddSeconds(tokenResponse.ExpirationInSeconds - ExpirationTimeBufferInSeconds);
                    }
                });
            }
        }
Ejemplo n.º 2
0
 public DevTestContext()
     : base("TestContext")
 {
     retryPolicy           = new RetryPolicy <SqlDatabaseTransientErrorDetectionStrategy>(RetryStrategyFactory.GetSqlDbContextRetryPolicy());
     retryPolicy.Retrying += (s, e) => Trace.TraceError("An error occurred in attempt number {1} to access the OrderContext: {0}", e.LastException.Message, e.CurrentRetryCount);
 }
Ejemplo n.º 3
0
        public bool JitterRetryStrategy_RetriesCountExceeded(int maxAttemptsCount, int currentAttempt)
        {
            var strategy = RetryStrategyFactory.Jitter(2);

            return(strategy.TryGetRetryDelay(3, out _));
        }