Toolbelt.Blazor.HttpClientInterceptor 9.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Toolbelt.Blazor.HttpClientInterceptor --version 9.0.0
NuGet\Install-Package Toolbelt.Blazor.HttpClientInterceptor -Version 9.0.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="Toolbelt.Blazor.HttpClientInterceptor" Version="9.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolbelt.Blazor.HttpClientInterceptor --version 9.0.0
#r "nuget: Toolbelt.Blazor.HttpClientInterceptor, 9.0.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.
// Install Toolbelt.Blazor.HttpClientInterceptor as a Cake Addin
#addin nuget:?package=Toolbelt.Blazor.HttpClientInterceptor&version=9.0.0

// Install Toolbelt.Blazor.HttpClientInterceptor as a Cake Tool
#tool nuget:?package=Toolbelt.Blazor.HttpClientInterceptor&version=9.0.0

Blazor WebAssembly (client-side) HttpClient Interceptor NuGet Package

Summary

The class library that intercept all of the sending HTTP requests on a client side Blazor WebAssembly application.

Supported Blazor versions

"Blazor WebAssembly App (client-side) HttpClient Interceptor" ver.9.x supports Blazor WebAssembly App version 3.2 Preview 2~5, and Release Candidate 1.

How to install and use?

Step.1 Install the library via NuGet package, like this.

> dotnet add package Toolbelt.Blazor.HttpClientInterceptor

Step.2 Register "HttpClientInterceptor" 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 when registration of HttpClient as a transient service to DI container.

public class Program
{
  public static async Task Main(string[] args)
  {
    ...
    builder.Services.AddTransient(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 HttpClientInterceptor service from the DI container.

@using Toolbelt.Blazor
@inject HttpClientInterceptor 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.
  }
  ...

Note: Please remember that if you use HttpClientInterceptor to subscribe BeforeSend/AfterSend events in Blazor components (.razor), you should unsubscribe events when the components is discarded. To do it, you should implement IDisposable interface in that component, like this code:

@implements IDisposable
...
public void Dispose()
{
  this.Interceptor.BeforeSend -= Interceptor_BeforeSend;
}

The arguments of event handler

The event handler for BeforeSend/AfterSend 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.
  }
}

Release Notes

Release notes is here.

License

Mozilla Public License Version 2.0

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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 (8)

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

GitHub repositories (2)

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 226,220 9/29/2022
10.1.0 132,590 2/18/2022
10.0.0 175,479 8/21/2021
9.2.1 96,689 11/10/2020
9.2.0-preview.1 1,033 9/26/2020
9.1.0 4,375 9/11/2020
9.1.0-preview.1 224 9/9/2020
9.0.1 45,364 7/22/2020
9.0.0 9,983 4/25/2020
8.0.1 3,348 3/12/2020
8.0.0 297 3/11/2020
7.0.0 11,597 1/29/2020
6.1.0 387 1/24/2020
6.0.0 3,695 11/20/2019
5.0.0 3,957 6/14/2019
4.0.0.3 1,384 4/26/2019
3.0.0 1,767 2/8/2019
2.1.0 1,945 10/3/2018
2.0.0 1,349 9/21/2018
1.0.0 1,615 9/14/2018

v.9.0.0
- BREAKING CHANGE: Support Blazor v.3.2.0 Preview 5 style HttpClient DI container registration.
v.8.0.1
- Fix: conflict "AddBaseAddressHttpClient()" service injection.
v.8.0.0
- BREAKING CHANGE: Support Blazor v.3.2.0 Preview 2 (not compatible with v.3.2.0 Preview 1 or before.)
v.7.0.0
- BREAKING CHANGE: Support Blazor v.3.2.0 Preview 1 (not compatible with v.3.1.0 Preview 4 or before.)
v.6.1.0
- The event handler arguments now provides a request object and a response object.
v.6.0.0
- BREAKING CHANGE: Support Blazor v.3.1.0 Preview 3 (not compatible with v.3.1.0 Preview 2 or before.)
v.5.0.0
- BREAKING CHANGE: Support Blazor v.3.0.0 Preview 6 (not compatible with v.3.0.0 Preview 5 or before.)
v.4.0.0
- BREAKING CHANGE: Support Blazor v.3.0.0 Preview 4 (not compatible with v.0.9.0 or before.)
v.3.0.0
- BREAKING CHANGE: Support Blazor v.0.8.0 (not compatible with v.0.7.0 or before.)
v.2.1.0
- Support Blazor v.0.6.0 - it was signed strong name.
v.2.0.0
- BREAKING CHANGE: Fix namespace of HttpClientInterceptorExtension class.
v.1.0.0
- 1st release.