Toolbelt.Blazor.HttpClientInterceptor 9.2.1

Intercept all of the sending HTTP requests on a client side Blazor application.

Install-Package Toolbelt.Blazor.HttpClientInterceptor -Version 9.2.1
dotnet add package Toolbelt.Blazor.HttpClientInterceptor --version 9.2.1
<PackageReference Include="Toolbelt.Blazor.HttpClientInterceptor" Version="9.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolbelt.Blazor.HttpClientInterceptor --version 9.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Toolbelt.Blazor.HttpClientInterceptor, 9.2.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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.2.1

// Install Toolbelt.Blazor.HttpClientInterceptor as a Cake Tool
#tool nuget:?package=Toolbelt.Blazor.HttpClientInterceptor&version=9.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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+, and Release Candidates, of course, 3.2.x official release, and .NET 5 is also supported.

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 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 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.
  }
  ...

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 HttpClientInterceptor to subscribe BeforeSend/BeforeSendAsync/AfterSend/AfterSendAsync 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/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.
  }
}

To read the content at "AfterSendAsync" event handler

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 the GetCapturedContentAsync() method, the LoadIntoBufferAsync() method of the Response.Content property 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();
  ...

Release Notes

Release notes is here.

License

Mozilla Public License Version 2.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+, and Release Candidates, of course, 3.2.x official release, and .NET 5 is also supported.

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 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 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.
  }
  ...

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 HttpClientInterceptor to subscribe BeforeSend/BeforeSendAsync/AfterSend/AfterSendAsync 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/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.
  }
}

To read the content at "AfterSendAsync" event handler

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 the GetCapturedContentAsync() method, the LoadIntoBufferAsync() method of the Response.Content property 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();
  ...

Release Notes

Release notes is here.

License

Mozilla Public License Version 2.0

Release Notes

v.9.2.1
- Fix: BeforeSend/AfterSend events are invoked twice in some cases.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Toolbelt.Blazor.HttpClientInterceptor:

Package Downloads
Toolbelt.Blazor.LoadingBar
Insert loading bar UI automatically into a client side Blazor application.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on Toolbelt.Blazor.HttpClientInterceptor:

Repository Stars
blazorhero/CleanArchitecture
Clean Architecture Template for Blazor WebAssembly Built with MudBlazor Components.
jsakamoto/Toolbelt.Blazor.LoadingBar
Loading bar UI for Client-Side Blazor application.

Version History

Version Downloads Last updated
9.2.1 14,847 11/10/2020
9.2.0-preview.1 639 9/26/2020
9.1.0 2,141 9/11/2020
9.1.0-preview.1 114 9/9/2020
9.0.1 17,813 7/22/2020
9.0.0 7,634 4/25/2020
8.0.1 2,409 3/12/2020
8.0.0 159 3/11/2020
7.0.0 6,918 1/29/2020
6.1.0 212 1/24/2020
6.0.0 2,926 11/20/2019
5.0.0 3,210 6/14/2019
4.0.0.3 421 4/26/2019
3.0.0 473 2/8/2019
2.1.0 1,035 10/3/2018
2.0.0 414 9/21/2018
1.0.0 445 9/14/2018