public static void AddApplicationServices(this IServiceCollection services, IConfiguration configuration)
 {
     services.AddHttpClient <ICoinStatisticsService, CoinStatisticsService>(client =>
     {
         client.DefaultRequestHeaders.Add(HttpRequestHeader.Accept.ToString(), MediaTypeNames.Application.Json);
         client.BaseAddress = new Uri(configuration["Apis:CoinExchange:Url"]);
     })
     .AddPolicyHandler(RetryPolicy.GetRetryPolicy());
 }
Exemple #2
0
        public static Forecast RetryPolicyExample()
        {
            /*
             *  This will try to execute the service, and retry immediately after a failed call.
             */
            var result = RetryPolicy.GetRetryPolicy()
                         .Execute(() => _weatherService.GetForecast(true));

            return(result);
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            //services.AddFluentValidation();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "WebJetMovies", Version = "v1"
                });
            });

            services.AddCors(options =>
            {
                options.AddPolicy("AllowAny",
                                  builder =>
                {
                    builder.WithOrigins("*");
                });
            });


            AssemblyScanner
            .FindValidatorsInAssembly(Assembly.GetExecutingAssembly())
            .ForEach(result => services.AddScoped(result.InterfaceType, result.ValidatorType));

            services.AddMediatR(Assembly.GetExecutingAssembly());
            services.AddHttpClient <IHttpClientWrapper, HttpClientWrapper>((client) =>
            {
                //Token that is retrieved from environment variables (not included in GitHub
                //and not sent to the UI for security. Instead app service has it set as an env variable)
                client.DefaultRequestHeaders.Add(ApiConstants.RequestHeaders.AccessTokenHeaderName, Configuration[ApiConstants.ConfigurationNames.AccessToken]);

                Uri.TryCreate(Configuration[ApiConstants.ConfigurationNames.ApiServerAddress], UriKind.Absolute, out Uri baseAddress);

                client.BaseAddress = baseAddress;
                client.Timeout     = TimeSpan.FromSeconds(double.Parse(Configuration[ApiConstants.ConfigurationNames.MaxDurationOfClientRequest]));
            }).SetHandlerLifetime(TimeSpan.FromMinutes(5))
            .AddPolicyHandler(RetryPolicy.GetRetryPolicy());

            services.AddScoped <IMovieApiClient, MovieApiClient>();

            services.Configure <MovieApiUris>(Configuration.GetSection(ApiConstants.ConfigurationNames.MovieApiUris));
            services.Configure <ApiRequestDetails>(Configuration.GetSection(ApiConstants.ConfigurationNames.ApiRequestDetails));

            services.AddApiVersioning(config =>
            {
                config.DefaultApiVersion = new ApiVersion(1, 0);
                config.AssumeDefaultVersionWhenUnspecified = true;
                config.ReportApiVersions = true;
                config.ApiVersionReader  = new HeaderApiVersionReader("api-version");
            });

            services.AddSingleton(Log.Logger);
        }
Exemple #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddSwaggerGen();

            services
            .AddHttpClient <ISearchEngineService, SearchEngineService>(c =>
            {
                c.BaseAddress = new Uri(Configuration["Google"]);
            })
            .SetHandlerLifetime(TimeSpan.FromMinutes(5))
            .AddPolicyHandler(RetryPolicy.GetRetryPolicy());
        }
Exemple #5
0
        public static Forecast WrapPoliciesExample()
        {
            var retryPolicy        = RetryPolicy.GetRetryPolicy(1);
            var retryAndWaitPolicy = RetryPolicy.GetRetryAndWaitPolicy(1);
            var fallbackPolicy     = FallbackPolicy.GetFallbackPolicy(_fallbackWeatherService.GetForecast);

            // Policy Wrap can combine multiple policies.
            // In this example, try retrying immediately first, then wait N seconds to retry, and if that fails, use a fallback.

            // Policy run order is from the latest wrapped to the first wrapped.
            // This will execute retry, then retryAndWait and finally fallback.
            var policyWrap = fallbackPolicy.Wrap(retryAndWaitPolicy).Wrap(retryPolicy);

            var result = policyWrap.Execute(() => _weatherService.GetForecast(true));

            return(result);
        }
Exemple #6
0
        public static IServiceCollection AddProcessPaymentUseCase(this IServiceCollection services)
        {
            services.TryAddScoped <ICryptoService, CryptoService>();
            services.TryAddScoped <IPaymentRepository, PaymentRepository>();
            services.TryAddScoped(typeof(IAggregateStore <>), typeof(AggreateStore <>));



            if (Environment.IsDevelopment())
            {
                services.AddHostedService <AcquiringBankMockService>();
            }

            services.AddHttpClient <IAquiringBankClient, AcquiringBankClient>()
            .AddPolicyHandler(RetryPolicy.GetRetryPolicy(2));

            return(services);
        }