Esempio n. 1
0
        public static void RegisterServices(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton <IValidationAttributeAdapterProvider, CpfValidationAttributeAdapterProvider>();

            services.AddTransient <HttpClientAuthorizationDelegatingHandler>();

            services.AddHttpClient <IAutenticacaoService, AutenticacaoService>();

            services.AddHttpClient <ICatalogoService, CatalogoService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>()
            //.AddTransientHttpErrorPolicy(
            //p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(600)));
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped <IUser, AspNetUser>();

            #region Refit

            //services.AddHttpClient("Refit",
            //        options =>
            //        {
            //            options.BaseAddress = new Uri(configuration.GetSection("CatalogoUrl").Value);
            //        })
            //    .AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
            //    .AddTypedClient(Refit.RestService.For<ICatalogoServiceRefit>);

            #endregion
        }
        public static void RegisterServices(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton <IValidationAttributeAdapterProvider, CpfValidationAttributeAdapterProvider>();

            services.AddTransient <HttpClientAuthorizationDelegatingHandler>();

            services.AddHttpClient <IAutenticacaoService, AutenticacaoService>();

            services.AddHttpClient <ICatalogoService, CatalogoService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>() //passa autorização do token para as API
            .AddPolicyHandler(PollyExtensions.EsperarTentar())                  //se gerar algum erro no middleware, tenta executar mais algumas vezes
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));       //fecha o circuito após 5 tentativas de acessar o middleware, para não acumular requests
            //.AddTransientHttpErrorPolicy(
            //p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(600)));

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped <IUser, AspNetUser>();

            //services.AddHttpClient("Refit", options =>
            //{
            //    options.BaseAddress = new Uri(configuration.GetSection("CatalogoUrl").Value);
            //})
            //.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
            //.AddTypedClient(Refit.RestService.For<ICatalogoServiceRefit>);
        }
        public static void RegisterServices(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton <IValidationAttributeAdapterProvider, CpfValidationAttributeAdapterProvider>();
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped <IAspNetUser, AspNetUser>();

            #region HttpServices
            services.AddTransient <HttpClientAuthorizationDelegateHandler>();

            services.AddHttpClient <IAutenticacaoService, AutenticacaoService>()
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));

            services.AddHttpClient <ICatalogoService, CatalogoService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegateHandler>()
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));

            services.AddHttpClient <ICarrinhoService, CarrinhoService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegateHandler>()
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));
            #endregion
        }
        public static void RegisterServices(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton <IValidationAttributeAdapterProvider, CpfValidationAttributeAdapterProvider>();
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped <IAspNetUser, AspNetUser>();

            #region HttpServices

            services.AddTransient <HttpClientAuthorizationDelegatingHandler>();

            services.AddHttpClient <IAutenticacaoService, AutenticacaoService>()
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));

            services.AddHttpClient <ICatalogoService, CatalogoService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>()
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(
                    handledEventsAllowedBeforeBreaking: 5, durationOfBreak: TimeSpan.FromSeconds(30)));

            services.AddHttpClient <IComprasBffService, ComprasBffService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>()
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(
                    handledEventsAllowedBeforeBreaking: 5, durationOfBreak: TimeSpan.FromSeconds(30)));

            services.AddHttpClient <IClienteService, ClienteService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>()
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(
                    handledEventsAllowedBeforeBreaking: 5, durationOfBreak: TimeSpan.FromSeconds(30)));



            #endregion


            #region Refit
            //services.AddHttpClient(name: "Refit", opions =>
            //     {
            //         opions.BaseAddress = new Uri(uriString: configuration.GetSection(key: "CatalogoUrl").Value);
            //     })
            //    .AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
            //    .AddTypedClient(Refit.RestService.For<ICatalogoServiceRefit>);
            #endregion
        }
        public static void RegisterServices(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddTransient <HttpClientAuthorizationDelegatingHandler>();

            services.AddHttpClient <IAutenticacaoService, AutenticacaoService>();

            services.AddHttpClient <ICatalogoService, CatalogoService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>()
            //.AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(600)));
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30))
                );

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped <IUser, AspNetUser>();
        }
Esempio n. 6
0
        public static void RegisterServices(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton<IValidationAttributeAdapterProvider,CpfValidationAttributeAdapterProvider>();

            //Registro Delegate
            services.AddTransient<HttpClientAuthorizationDelegatingHandler>();

            services.AddHttpClient<IAutenticacaoService, AutenticacaoService>();

            services.AddHttpClient<ICatalogoService, CatalogoService>()
                .AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()//Delegate - Intercepta qualquer request q vem Desse serviço
                //.AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(1))); //policy
                .AddPolicyHandler(PollyExtensions.EsperarTentar()) //Policy de tentativa caso ocorra falhas de rede 
                .AddTransientHttpErrorPolicy(p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));// o CircuitBreaker conta a requisição como um todo da aplicação (nao é por usuario) 

            #region example Refit

            // Trabalhando com Refit no CatalogoService

            //services.AddHttpClient("Refit", options =>         //cofigura BaseAdress e IOptions<AppSettings> settings
            //{
            //    options.BaseAddress = new Uri(configuration.GetSection("CatalogoUrl").Value);
            //})
            //.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
            //{
            //    ClientCertificateOptions = ClientCertificateOption.Manual, //ignorando o certificado da maquina
            //    ServerCertificateCustomValidationCallback =
            //        (httpRequestMessage, cert, cetChain, policyErrors) =>
            //        {
            //            return true;
            //        }
            //})
            //.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>() //Intercepta qualquer request q vem desse serviço
            //.AddTypedClient(Refit.RestService.For<ICatalogoServiceRefit>); //  cria o typeClient em tempo de execução

            #endregion

            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            services.AddScoped<IUser, AspNetUser>();
        }
Esempio n. 7
0
        public static IServiceCollection ResolveDependencies(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton <IValidationAttributeAdapterProvider, CpfValidationAttributeAdapterProvider>();
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped <IAspNetUser, AspNetUser>();

            #region HttpServices

            services.AddTransient <HttpClientAuthorizationDelegatingHandler>();

            services.AddHttpClient <IAutenticacaoService, AutenticacaoService>()
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AllowSelfSignedCertificate()
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));

            services.AddHttpClient <ICatalogoService, CatalogoService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>()
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AllowSelfSignedCertificate()
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));

            services.AddHttpClient <IComprasBffService, ComprasBffService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>()
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AllowSelfSignedCertificate()
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));

            services.AddHttpClient <IClienteService, ClienteService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>()
            .AddPolicyHandler(PollyExtensions.EsperarTentar())
            .AllowSelfSignedCertificate()
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));

            #endregion

            return(services);
        }
Esempio n. 8
0
        public static void RegisterServices(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddTransient <HttpClientAuthorizationDelegatingHandler>();

            services.AddHttpClient <IAuthenticationAPIService, AuthenticationAPIService>();

            var retryPolicy = HttpPolicyExtensions
                              .HandleTransientHttpError()
                              .WaitAndRetryAsync(new[]
            {
                TimeSpan.FromSeconds(1),
                TimeSpan.FromSeconds(5),
                TimeSpan.FromSeconds(10)
            }, (outcome, timespan, retryCount, context) =>
            {
                Console.WriteLine($"Retry for the {retryCount} time");
            });

            services.AddHttpClient <ICatalogService, CatalogService>()
            .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>()
            //.AddTransientHttpErrorPolicy(
            //    p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(600)));
            .AddPolicyHandler(PollyExtensions.WaitAndRetry())
            .AddTransientHttpErrorPolicy(
                p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));

            #region Refit
            //services.AddHttpClient("Refit",
            //        options =>
            //        {
            //            options.BaseAddress = new Uri(configuration.GetSection("CatalogUrl").Value);
            //        })
            //    .AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
            //    .AddTypedClient(Refit.RestService.For<ICatalogServiceRefit>);
            #endregion


            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped <IUser, AspNetUser>();
        }
        public static void RegisterServices(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton <IValidationAttributeAdapterProvider, CpfValidationAttributeAdapterProvider>();

            services.AddTransient <HttpClientAuthorizationDelegatingHandler>();

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped <IUser, AspNetUser>();

            services.AddHttpClient <IAutenticacaoService, AutenticacaoService>()
            .ConfigureHttpMessageHandlerBuilder(x =>
            {
                x.PrimaryHandler = new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback =
                        HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
                };
            });


            var catalogoUrl = configuration.GetSection("CatalogoUrl").Value;

            //var settings = new RefitSettings(new SystemTextJsonContentSerializer());

            services.AddHttpClient("Refit",
                                   config => { config.BaseAddress = new Uri(catalogoUrl); })
            .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>()
            .AddTypedClient(RestService.For <ICatalogoServiceRefit>)
            .AddPolicyHandler(PollyExtensions.TentarRequisicaoNovamente())
            .AddTransientHttpErrorPolicy(p =>
                                         p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)))
            .ConfigureHttpMessageHandlerBuilder(x =>
            {
                x.PrimaryHandler = new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback =
                        HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
                };
            });
        }