public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { services.AddSingleton <IReadOnlyPolicyRegistry <string> >(PollyPolicyRegistry.Create()); services.AddTransient <IReportSaver, TxtReportSaver>(); // Add Hangfire services. services.AddHangfire(cfg => cfg .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .UseSimpleAssemblyNameTypeSerializer() .UseRecommendedSerializerSettings() .UseSqlServerStorage(configuration.GetConnectionString("SqlConnection"), new SqlServerStorageOptions { CommandBatchMaxTimeout = TimeSpan.FromMinutes(5), SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5), QueuePollInterval = TimeSpan.Zero, UseRecommendedIsolationLevel = true, UsePageLocksOnDequeue = true, DisableGlobalLocks = true })); // Add the processing server as IHostedService services.AddHangfireServer(); return(services); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //Polly Related Configuration services.AddMemoryCache(); services.AddSingleton <Polly.Caching.IAsyncCacheProvider, Polly.Caching.Memory.MemoryCacheProvider>(); var policyRegistry = services.AddPolicyRegistry(); PollyPolicyRegistry.Build(policyRegistry); //Policy Holder , Alternative of Policy Registry services.AddSingleton <IPolicyHolder, PolicyHolder>(); //HttpClient HttpClient httpClient = new HttpClient() { BaseAddress = new Uri(BASE_API) // this is the endpoint HttpClient will hit, }; httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); services.AddSingleton <HttpClient>(httpClient); //HttpClient Factory //services.AddSingleton<IReadOnlyPolicyRegistry>(registry); services.AddHttpClient("OrderService", client => { client.BaseAddress = new Uri(BASE_API); // this is the endpoint HttpClient will hit, client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); }).AddPolicyHandlerFromRegistry(PollyPolicyRegistry.TimeoutPolicy) .AddPolicyHandlerFromRegistry(PollyPolicyRegistry.FallbackWithTimedOutExceptionPolicy) .AddPolicyHandlerFromRegistry((policyRegistry, httpRequestMessage) => { if (httpRequestMessage.Method == HttpMethod.Get) { return(policyRegistry.Get <IAsyncPolicy <HttpResponseMessage> >(PollyPolicyRegistry.BasicRetryPolicy)); } else { return(policyRegistry.Get <IAsyncPolicy <HttpResponseMessage> >(PollyPolicyRegistry.WaitAndRetryPolicy)); } }); services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Polly.API.Playground", Version = "v1" }); }); }