Http.Client.Options 7.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Http.Client.Options --version 7.1.0
                    
NuGet\Install-Package Http.Client.Options -Version 7.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Http.Client.Options" Version="7.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Http.Client.Options" Version="7.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Http.Client.Options" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Http.Client.Options --version 7.1.0
                    
#r "nuget: Http.Client.Options, 7.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Http.Client.Options@7.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Http.Client.Options&version=7.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Http.Client.Options&version=7.1.0
                    
Install as a Cake Tool

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
              };
          });

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
8.0.0-beta.63 287 4/12/2023
8.0.0-beta.62 136 4/12/2023
8.0.0-beta.61 123 4/12/2023
8.0.0-beta.60 126 4/12/2023
8.0.0-beta.57 127 4/12/2023
8.0.0-beta.20 126 4/13/2023
8.0.0-beta.18 124 4/13/2023
8.0.0-beta.17 121 4/13/2023
8.0.0-beta.10 124 4/12/2023
8.0.0-beta.9 125 4/12/2023
8.0.0-beta.1 131 3/13/2023
8.0.0-alpha.10 121 4/13/2023
7.2.3-alpha 166 11/4/2022
7.2.2-alpha 149 11/3/2022
7.2.1-alpha 154 11/3/2022
7.2.0 54,965 4/27/2022
7.2.0-alpha 145 11/3/2022
7.1.0 198 4/25/2022
7.0.1 203 4/25/2022
6.0.1 16,628 2/21/2022
6.0.0.4 4,982 1/3/2022
6.0.0.3 357 1/3/2022
6.0.0.2 351 1/3/2022
6.0.0.1 13,138 11/10/2021
5.0.6 415 11/10/2021
5.0.0.2 390 11/10/2021
5.0.0.1 413 11/10/2021
5.0.0.1-preview 321 11/10/2021