Http.Client.Options
8.0.0-beta.63
dotnet add package Http.Client.Options --version 8.0.0-beta.63
NuGet\Install-Package Http.Client.Options -Version 8.0.0-beta.63
<PackageReference Include="Http.Client.Options" Version="8.0.0-beta.63" />
paket add Http.Client.Options --version 8.0.0-beta.63
#r "nuget: Http.Client.Options, 8.0.0-beta.63"
// Install Http.Client.Options as a Cake Addin #addin nuget:?package=Http.Client.Options&version=8.0.0-beta.63&prerelease // Install Http.Client.Options as a Cake Tool #tool nuget:?package=Http.Client.Options&version=8.0.0-beta.63&prerelease
Purpurse
Simplified 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;
});
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;
});
Open Telemetry
Use Open Telemetry and Http Instrumentation to trace the requests
Configure Open Telemetry builder
Tracing support request details, response, timing, and config, see more on how to customize open telemetry here
serviceCollection.AddHttpOptionsTelemetry(optionsBuilder =>
{
optionsBuilder.ConfigureOpenTelemetryBuilder(builder => builder.AddConsoleExporter());
}
);
Activity details example
Activity.Id: 00-057d78264f60ab804d6673a9c6d66fe9-5d99a746899f034a-01
Activity.DisplayName: http-options-activity
Activity.Kind: Client
Activity.StartTime: 2022-04-25T21:13:18.7215742Z
Activity.Duration: 00:00:00.2143088
Activity.TagObjects:
config.server: 127.0.0.1
config.port: 64578
config.schema: http
config.name: service
config.timeout: -1
config.handler.maxConnection: 100
config.handler.lifeTimeMinutes: 10
correlation.id: a5262536662549b28e23a1c6ffae1dc9
timestamp: 7276724051916
http.method: GET
http.url: http://127.0.0.1:64578/delay/200ms
http.scheme: http
http.target: 127.0.0.1
http.route: /delay/200ms
host.port: 64578
http.status_code: 200
time.start: 25/04/2022 21:13:18
time.end: 25/04/2022 21:13:18
time.duration: 00:00:00.2143088
time.http.start: 25/04/2022 21:13:18
time.http.end: 25/04/2022 21:13:18
time.http.duration: 00:00:00.2141251
time.delta.start.ms: 0.0156
time.delta.end.ms: 0.1681
time.delta.ms: 0.1837
http.host: 127.0.0.1:64578
otel.status_code: UNSET
Resource associated with Activity:
service.name: http-options-service
service.instance.id: 6f61406e-5267-43be-9a11-75b57173b15c
Customize tracing tags
Customize the tags by simply set the tag to any string
serviceCollection.AddHttpOptionsTelemetry(optionsBuilder =>
{
optionsBuilder.ConfigureTags(tagsOptions =>
{
tagsOptions.Config.Name = "name";
tagsOptions.Config.Port = "port";
tagsOptions.Config.Schema = "schema";
tagsOptions.Config.MaxConnection = "maxConnection";
tagsOptions.Request.Schema = "r.schema";
tagsOptions.Request.RequestLength = "size";
tagsOptions.Request.RequestPath = "path";
tagsOptions.Request.Host = "host";
});
optionsBuilder.ConfigureBuilder(builder => builder.AddConsoleExporter());
}
);
Activity tags example
Activity.TagObjects:
config.server: 127.0.0.1
port: 54347
schema: http
name: service
config.timeout: -1
maxConnection: 100
r.schema: http
host: 127.0.0.1
path: /delay/200ms
#... other tags as usual
Customize Enrichment
We are using Http Instrumentation to add tags about the request details, plus to additional tags provided by this library, but you can easily add any tag you want on request, response, or error events.
optionsBuilder.ConfigureEnrichment(enrichmentOptions =>
{
enrichmentOptions.OnRequest((activity, message) =>
{
activity.Tags["auth-custom-tag"] = message.AuthenticationLevel;
});
enrichmentOptions.OnResponse((activity, message) =>
{
activity.Tags["response.cookies.count"] = message.Cookies.Count;
});
enrichmentOptions.OnError((activity, message) =>
{
activity.Tags["error.source"] = message.Source;
});
}
);
Customize Processor
While enrichment is called via http events, processor wil be called just as activity is starting and after all events are done, before sending the activity to the exporter.
Open Telemetry Processor
To add any open telemetry processor), simply configure telemetry with the processor.
optionsBuilder.ConfigureOpenTelemetryBuilder(builder => builder.AddProcessor(new MyProcessor()));
Add action as processor
Allowing simple actions to start and end of activities as actions
optionsBuilder.ConfigureProcessor(builder =>
{
builder
.OnActivityStart(activityContext =>
{
activityContext.Activity.SetTag("just-marker", "my-cool-service-did-it") ;
});
builder
.OnActivityEnd(activityContext =>
{
activityContext.Activity.SetTag("is-long-request", activityContext.Activity.Duration > TimeSpan.FromHours(1)) ;
});
});
Use activity properties to add data to be carried along the request .
optionsBuilder.ConfigureProcessor(builder =>
{
builder
.OnActivityStart(activityContext =>
{
var counter = Counters.StartNewActivity(activityContext.ClientOptions.ServiceName);
activityContext.Activity.SetCustomProperty("request-counter", counter);
});
builder
.OnActivityEnd(activityContext =>
{
var counter =
activityContext.Activity.GetCustomProperty("request-counter") as RequestCounter;
activityContext.Tags["request-counts"] = counter?.RequestEnd();
});
});
Customize Exporter
Open Telemetry Exporter
To add any Open Telemetry Exporter), simply configure the telemetry with the exporter.
serviceCollection.AddHttpOptionsTelemetry(optionsBuilder =>
{
optionsBuilder.ConfigureOpenTelemetryBuilder(builder => builder.AddConsoleExporter());
}
);
If you don't need a special open telemetry exporter, you can add any simple export action to be happened in export.
serviceCollection.AddHttpOptionsTelemetry(optionsBuilder =>
{
optionsBuilder.ConfigureExportAction(activity => _activities.Add(activity));
}
Note that export actions will be called after all processing are done, don't change the activity at that point
Customize Activity Settings
optionsBuilder.ConfigureTracing(options =>
{
options.Activity.Source = new ActivitySource("my-source");
options.Activity.ActivityName = "http-clint-activity";
options.Activity.ActivityService = "order-service";
});
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");
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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 is compatible. 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. |
-
net6.0
- Microsoft.ApplicationInsights (>= 2.17.0)
- Microsoft.Extensions.DependencyInjection (>= 3.1.24)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.24)
- Microsoft.Extensions.Http (>= 3.1.0)
- Microsoft.Extensions.Http.Polly (>= 3.1.0)
- OpenTelemetry (>= 1.4.0)
- OpenTelemetry.Extensions.Hosting (>= 1.4.0)
- OpenTelemetry.Instrumentation.Http (>= 1.0.0-rc9.14)
- Polly (>= 7.1.0)
- Polly.Extensions.Http (>= 3.0.0)
- System.ComponentModel.Annotations (>= 4.7.0)
- System.Diagnostics.Tracing (>= 4.3.0)
-
net7.0
- Microsoft.ApplicationInsights (>= 2.17.0)
- Microsoft.Extensions.DependencyInjection (>= 3.1.24)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.24)
- Microsoft.Extensions.Http (>= 3.1.0)
- Microsoft.Extensions.Http.Polly (>= 3.1.0)
- OpenTelemetry (>= 1.4.0)
- OpenTelemetry.Extensions.Hosting (>= 1.4.0)
- OpenTelemetry.Instrumentation.Http (>= 1.0.0-rc9.14)
- Polly (>= 7.1.0)
- Polly.Extensions.Http (>= 3.0.0)
- System.ComponentModel.Annotations (>= 4.7.0)
- System.Diagnostics.Tracing (>= 4.3.0)
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 | 131 | 4/12/2023 |
8.0.0-beta.62 | 93 | 4/12/2023 |
8.0.0-beta.61 | 78 | 4/12/2023 |
8.0.0-beta.60 | 83 | 4/12/2023 |
8.0.0-beta.57 | 82 | 4/12/2023 |
8.0.0-beta.20 | 82 | 4/13/2023 |
8.0.0-beta.18 | 82 | 4/13/2023 |
8.0.0-beta.17 | 79 | 4/13/2023 |
8.0.0-beta.10 | 80 | 4/12/2023 |
8.0.0-beta.9 | 80 | 4/12/2023 |
8.0.0-beta.1 | 90 | 3/13/2023 |
8.0.0-alpha.10 | 76 | 4/13/2023 |
7.2.3-alpha | 124 | 11/4/2022 |
7.2.2-alpha | 107 | 11/3/2022 |
7.2.1-alpha | 109 | 11/3/2022 |
7.2.0 | 25,045 | 4/27/2022 |
7.2.0-alpha | 100 | 11/3/2022 |
7.1.0 | 136 | 4/25/2022 |
7.0.1 | 135 | 4/25/2022 |
6.0.1 | 14,602 | 2/21/2022 |
6.0.0.4 | 4,900 | 1/3/2022 |
6.0.0.3 | 286 | 1/3/2022 |
6.0.0.2 | 281 | 1/3/2022 |
6.0.0.1 | 11,634 | 11/10/2021 |
5.0.6 | 342 | 11/10/2021 |
5.0.0.2 | 317 | 11/10/2021 |
5.0.0.1 | 339 | 11/10/2021 |
5.0.0.1-preview | 253 | 11/10/2021 |
CHANGELOG.md