![]() |
VOOZH | about |
dotnet add package Toolbelt.Blazor.HttpClientInterceptor --version 10.2.0
NuGet\Install-Package Toolbelt.Blazor.HttpClientInterceptor -Version 10.2.0
<PackageReference Include="Toolbelt.Blazor.HttpClientInterceptor" Version="10.2.0" />
<PackageVersion Include="Toolbelt.Blazor.HttpClientInterceptor" Version="10.2.0" />Directory.Packages.props
<PackageReference Include="Toolbelt.Blazor.HttpClientInterceptor" />Project file
paket add Toolbelt.Blazor.HttpClientInterceptor --version 10.2.0
#r "nuget: Toolbelt.Blazor.HttpClientInterceptor, 10.2.0"
#:package Toolbelt.Blazor.HttpClientInterceptor@10.2.0
#addin nuget:?package=Toolbelt.Blazor.HttpClientInterceptor&version=10.2.0Install as a Cake Addin
#tool nuget:?package=Toolbelt.Blazor.HttpClientInterceptor&version=10.2.0Install as a Cake Tool
The class library that intercept all of the sending HTTP requests on a client side Blazor WebAssembly application.
"Blazor WebAssembly App (client-side) HttpClient Interceptor" ver.9.x supports Blazor WebAssembly App version 3.2 Preview 2+, and Release Candidates, of course, 3.2.x official release, and .NET 5, 6, 7 are also supported.
Step.1 Install the library via NuGet package, like this.
> dotnet add package Toolbelt.Blazor.HttpClientInterceptor
Step.2 Register IHttpClientInterceptor service into the DI container, at Main() method in the Program class of your Blazor application.
using Toolbelt.Blazor.Extensions.DependencyInjection; // <- Add this, and...
...
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddHttpClientInterceptor(); // <- Add this!
...
Step.3 Add invoking EnableIntercept(IServiceProvider) extension method at the registration of HttpClient to DI container.
public class Program
{
public static async Task Main(string[] args)
{
...
builder.Services.AddScoped(sp => new HttpClient {
BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
}.EnableIntercept(sp)); // <- Add this!
...
That's all.
You can subscribe the events that will occur before/after sending all of the HTTP requests, at anywhere you can get IHttpClientInterceptor service from the DI container.
@using Toolbelt.Blazor
@inject IHttpClientInterceptor Interceptor
...
@code {
protected override void OnInitialized()
{
this.Interceptor.BeforeSend += Interceptor_BeforeSend;
...
}
void Interceptor_BeforeSend(object sender, HttpClientInterceptorEventArgs e)
{
// Do something here what you want to do.
}
...
If you want to do async operations inside the event handler, please subscribe async version events such as BeforeSendAsync and AfterSendAsync, instead of BeforeSend and AfterSend.
Note: Please remember that if you use
IHttpClientInterceptorto subscribeBeforeSend/BeforeSendAsync/AfterSend/AfterSendAsyncevents in Blazor components (.razor), you should unsubscribe events when the components is discarded. To do it, you should implementIDisposableinterface in that component, like this code:
@implements IDisposable
...
public void Dispose()
{
this.Interceptor.BeforeSend -= Interceptor_BeforeSend;
}
The event handler for BeforeSend/BeforeSendAsync/AfterSend/AfterSendAsync events can be received HttpClientInterceptorEventArgs object.
The HttpClientInterceptorEventArgs object provides you to a request object and a response object that is come from an intercepted HttpClinet request.
void OnAfterSend(object sender, HttpClientInterceptorEventArgs args)
{
if (!args.Response?.IsSuccessStatusCode) {
// Do somthing here for handle all errors of HttpClient requests.
}
}
If you want to read the content in the Response object at the event handler for AfterSendAsync event, don't reference the Response.Content property directly to do it.
Instead, please use the return value of the GetCapturedContentAsync() method.
Note: Please remember that the
GetCapturedContentAsync()method has a little bit performance penalty.
Because in theGetCapturedContentAsync()method, theLoadIntoBufferAsync()method of theResponse.Contentproperty is invoked.
async Task OnAfterSendAsync(object sender, HttpClientInterceptorEventArgs args)
{
// 👇 Don't reference "args.Response.Content" directly to read the content.
// var content = await args.Response.Content.ReadAsStringAsync()
// 👇 Instead, please use the return value of the "GetCapturedContentAsync()" method.
var capturedContent = await arg.GetCapturedContentAsync();
var content = await capturedContent.ReadAsStringAsync();
...
If you want to cancel the request before sending it, you can do it by setting false to the Cancel property of the BeforeSend/BeforeSendAsync event argument.
void OnBeforeSend(object sender, HttpClientInterceptorEventArgs args)
{
if (/*something wron*/) {
// 👇 Setting the "Cancel" to true will cancel sending a request.
args.Cancel = true;
}
}
In that case, a HttpClient object will return the HttpResponseMessage object with HTTP status code "NoContent" (HTTP 204) to the caller, and also the AfterSend/AfterSendAsync event will not be fired.
Release notes is here.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on Toolbelt.Blazor.HttpClientInterceptor:
| Package | Downloads |
|---|---|
|
Toolbelt.Blazor.LoadingBar
Insert loading bar UI automatically into a Blazor WebAssembly app. |
|
|
SunnyMehr.P1.HttpClient
Package Description |
|
|
M5x.MudBlazor
Package Description |
|
|
SoftwareDriven.OpenIdConnect.Client
SoftwareDriven.OpenIdConnect.Client is a library to provide client side services to use the OIDC protocol. |
|
|
mdimai666.Blast.AppFront.Shared
Shared project using Blast client apps |
Showing the top 2 popular GitHub repositories that depend on Toolbelt.Blazor.HttpClientInterceptor:
| Repository | Stars |
|---|---|
|
fullstackhero/blazor-starter-kit
Clean Architecture Template for Blazor WebAssembly Built with MudBlazor Components.
|
|
|
jsakamoto/Toolbelt.Blazor.LoadingBar
Loading bar UI for Client-Side Blazor application.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 10.2.0 | 691,039 | 9/29/2022 |
| 10.1.0 | 163,296 | 2/18/2022 |
| 10.0.0 | 297,756 | 8/21/2021 |
| 9.2.1 | 113,932 | 11/10/2020 |
| 9.2.0-preview.1 | 1,263 | 9/26/2020 |
| 9.1.0 | 4,989 | 9/11/2020 |
| 9.1.0-preview.1 | 442 | 9/9/2020 |
| 9.0.1 | 64,500 | 7/22/2020 |
| 9.0.0 | 10,361 | 4/25/2020 |
| 8.0.1 | 5,525 | 3/12/2020 |
| 8.0.0 | 551 | 3/11/2020 |
| 7.0.0 | 11,937 | 1/29/2020 |
| 6.1.0 | 643 | 1/24/2020 |
| 6.0.0 | 4,005 | 11/20/2019 |
| 5.0.0 | 4,310 | 6/14/2019 |
| 4.0.0.3 | 1,726 | 4/26/2019 |
| 3.0.0 | 2,130 | 2/8/2019 |
| 2.1.0 | 2,293 | 10/3/2018 |
| 2.0.0 | 1,693 | 9/21/2018 |
| 1.0.0 | 2,402 | 9/14/2018 |
v.10.2.0
- Improve: GetCapturedContentAsync() will proceed even if the response has illegal content headers.