Skip to content

GigyaRnDDevOps/Http.Options

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Purpurse

Simplefied usage of http client factory using Options pattern

Configuration options

All configuration are disabled by default

Connection

Http client base url properties

option value
server The domain name of the base url
port The port part of the base url
schema The schema part of the base url (http/ https)
timeout Connection timeout, will be set as http client timeout
 serviceCollection.AddHttpClientOptions(options =>
            {
                options.ServiceName = "jsonplaceholder";
                options.ConnectionOptions.Server = "jsonplaceholder.typicode.com";
                options.ConnectionOptions.Schema = "http";
                options.ConnectionOptions.Port = 80;
                options.ConnectionOptions.Timeout = Timeout; 
            });

Http Client Handler

The http client heandler properties

option value
MaxConnection The maximum number of concurrent connections (per server endpoint)
HandlerLifeTimeMinutes The length of time that a HttpMessageHandler instance can be reused
  serviceCollection.AddHttpClientOptions(options =>
            {
                options.ServiceName = "my-service"; 
                options.HttpClientHandlerOptions.MaxConnection = 1;
                options.HttpClientHandlerOptions.HandlerLifeTimeMinutes = 10;
            });

Telemetry

Enable or disable metrix options

option value
Counter Enable count of total requests, active request, sucesses,and errors
Timing Enable timing of the http requests
       serviceCollection.AddHttpClientOptions(options =>
            {
                options.ServiceName = "service";
             
                options.TelemetryOptions.Counter = true;
                options.TelemetryOptions.Timing = true;
  
            });
            

Polly Options

All policies are disabled by default

Bulkhead

  
            serviceCollection.AddHttpClientOptions(options =>
            {
                options.ServiceName = "service";

                options.PollyOptions.Bulkhead.Enabled = true;
                options.PollyOptions.Bulkhead.MaxParallelization = 100;
                options.PollyOptions.Bulkhead.MaxQueuingActions = 10000;


            });

Retry

  
            serviceCollection.AddHttpClientOptions(options =>
            {
                options.ServiceName = "service";

                options.PollyOptions.Retry.Enabled = true;
                options.PollyOptions.Retry.Count = 5;
                options.PollyOptions.Retry.BackoffPower = 3;
                options.PollyOptions.Retry.MaxJitter = 100;


            });

CircuitBreaker

  
            serviceCollection.AddHttpClientOptions(options =>
            {
                options.ServiceName = "service";

                options.PollyOptions.CircuitBreaker.Enabled = true;
                options.PollyOptions.CircuitBreaker.FailureThreshold = 0.7;
                options.PollyOptions.CircuitBreaker.MinimumThroughput = 20;
                options.PollyOptions.CircuitBreaker.SamplingDuration = 1000;



            });

Timeout

  
            serviceCollection.AddHttpClientOptions(options =>
            {
                options.ServiceName = "service";

                options.PollyOptions.Timeout.Enabled = true;
                options.PollyOptions.Timeout.TimeoutMS = 1000; 
            });

Binding Options

Bind http Client by type

       serviceCollection.AddHttpClientOptions<ServiceClient>(options =>
            {
                options.ServiceName = "service";
             
                options.TelemetryOptions.Counter = true;
                options.TelemetryOptions.Timing = true;
  
            });
            

Usage of named client

  var factory = serviceProvider.GetRequiredService<IHttpClientFactory>();
  var client = factory.CreateClient("service");
  await client.GetAsync("todos/1");
   

Usage of typed client

class ServiceClient
       {
           private readonly HttpClient _httpClient;

           public ServiceClient(HttpClient httpClient)
           {
               _httpClient = httpClient;
           }

           public Task<HttpResponseMessage> Get()
           {
               return _httpClient.GetAsync("todos/1");
           }
       }

Runtime configuration source

     serviceCollection.AddHttpClientOptions(options =>
          {
              options.ServiceName = "service";
              options.ConnectionOptions.Provider = () => new HttpConnectionOptions()
              {
                  Server = ExternalProvider.Get<HttpServiceProps>().Domain
              };
              options.PollyOptions.Provider = () => new HttpPollyOptions()
              {
                  Retry = ExternalProvider.Get<HttpServiceProps>().Retry
              };
              
              options.HttpClientHandlerOptions.Provider = () => new HttpClientHandlerOptions()
              {
                  MaxConnection = ExternalProvider.Get<HttpServiceProps>().MaxConnection
              };
          });

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.8%
  • Batchfile 0.2%