Esempio n. 1
0
        /// <summary>
        /// Get a configured policy used for HttpClients
        /// </summary>
        /// <returns></returns>
        private static IAsyncPolicy <HttpResponseMessage> GetRetryPolicy(TvMazeConfig config)
        {
            // Using a Jitterer can improve the overall performance of the end-to-end system by adding randomness to the exponential backoff.
            // This spreads out the spikes when issues arise
            var jitterer    = new Random();
            var jitterValue = config.UseJitterer ? TimeSpan.FromMilliseconds(jitterer.Next(0, 100)) : new TimeSpan(0);

            return(HttpPolicyExtensions
                   .HandleTransientHttpError()
                   .OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
                   .WaitAndRetryAsync(config.RetryCount, // exponential back-off plus some jitter
                                      retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)) + jitterValue));
        }
Esempio n. 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <DatabaseContext>(options =>
                                                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            var config = new TvMazeConfig();

            Configuration.GetSection("TvMaze").Bind(config);

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddHttpClient <ITvMazeWebService, TvMazeWebService>(client => client.BaseAddress = new Uri(config.BaseUrl))
            .SetHandlerLifetime(TimeSpan.FromMinutes(config.HttpMessageHandlerLifeTime))
            .AddPolicyHandler(GetRetryPolicy(config));

            services.AddTransient <IShowService, ShowService>();
        }