
This article is part of a series called Apizr:
- Apizr – Part 1: A Refit based web api client, but resilient
- Apizr – Part 2: Resilient core features
- Apizr – Part 3: More advanced features (this one)
- Apizr – Part 4: Requesting with Mediator pattern
- Apizr – Part 5: Requesting with Optional pattern
After Part 1 with general presentation and Part 2 with core features walk-through, this time we’ll go deeper into some advanced scenarios.
// For both ways builder.WithBaseAddress(Constants.MyConfigurationAwareAddress) // Or for static way builder.WithBaseAddress(() => GetMyConfigurationAwareAddress()) // Or for extended way: builder.WithBaseAddress(serviceProvider => serviceProvider.GetService<MySettingsService>().MyConfigurationAwareAddress)
// For both ways builder => builder.AddDelegatingHandler(new MyCustomDelegatingHandler()) // Or for static way (with a logHandler instance to log all from your handler) builder => builder.AddDelegatingHandler(logHandler => new MyCustomDelegatingHandler(logHandler)) // Or for extended way builder => builder.AddDelegatingHandler(serviceProvider => new MyCustomDelegatingHandler(serviceProvider.GetService<ILogHandler>()))
// For static way builder => builder.WithHttpClientHandler(new MyCustomHttpClientHandler()) // Or extended way builder => builder.WithHttpClientHandler(serviceProvider => new MyCustomHttpClientHandler())
This one gives you access to several deep settings.
This is where you can deal with cookies, decompression, proxy, certificate validation and many more…
Something like this dummy example:
builder => builder.WithHttpClientHandler(serviceProvider => new HttpClientHandler { CookieContainer = serviceProvider.GetRequiredService<ISessionService>().Cookies, #if DEBUG ServerCertificateCustomValidationCallback = (message, certificate, chain, sslPolicyErrors) => true #endif })
The extended way let you adjust more and more HttpClient settings with the HttpClientBuilder:
// For extended way only builder => builder.ConfigureHttpClientBuilder(httpClientBuilder => httpClientBuilder.WhateverSettings)
Use this one with caution as Apizr makes use of it, so you could override things that could leads to unstable experience.
This is where you can configure complexe policies for example, like:
var timeout = Policy.TimeoutAsync<HttpResponseMessage>( TimeSpan.FromSeconds(10)); var longTimeout = Policy.TimeoutAsync<HttpResponseMessage>( TimeSpan.FromSeconds(30)); builder => builder.ConfigureHttpClientBuilder(httpCientBuilder => httpCientBuilder.AddPolicyHandler(request => request.Method == HttpMethod.Get ? timeout : longTimeout))
You can provide your own RefitSettings:
// For both ways builder.WithRefitSettings(new RefitSettings()) // Or for static way builder.WithRefitSettings(() => new RefitSettings()) // Or for extended way: builder.WithRefitSettings(serviceProvider => new RefitSettings())
RefitSettings let you adjust some Refit parameters like the ContentSerializer, the UrlParameterFormatter or the FormUrlEncodedParameterFormatter.
Something like:
builder => builder.WithRefitSettings(new RefitSettings(new NewtonsoftJsonContentSerializer(new JsonSerializerSettings { Error = delegate(object sender, ErrorEventArgs args) { Logger.Write(args.ErrorContext.Error)(); args.ErrorContext.Handled = true; }, Converters = {new IsoDateTimeConverter()} })))
Please refer to Refit official documentation to know more about it.
AddApizrFor<IMyAwesomeApi, MyAwsomeManager<IMyAwesomeApi>>>()