TheDanielDoyle.HostFilters 2.0.1

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

// Install TheDanielDoyle.HostFilters as a Cake Tool
#tool nuget:?package=TheDanielDoyle.HostFilters&version=2.0.1

A framework for executing code before your .NET Core / ASP.NET Core application runs.

Host filters will run before the IHost runs. It's useful for executing migrations or performing validation tasks before you allow the IHost to run.

Quick start

  1. Implement IHostFilter in a class, or derive from the built-in HostFilter or AsyncHostFilter base classes.
  2. Register all concrete types implementing IHostFilter in dependency injection.
  3. Replace RunAsync() with RunWithFiltersAsync() in your Program.cs Main method.
  4. Run the application and laugh like Dr. Evil.

Registering Host Filters

You can register host filters using the provided AddHostFilters() method, with its overloads, or use your own Dependency Injection container to match up IHostFilter and concrete types.

AddHostFilters is an extension available for IServiceProvider, with overloads to accomodate each need. See below.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHostFilters();
    services.AddHostFilters(typeof(Program).Assembly);
    services.AddHostFilter<HappyEasterHostFilter>();
    services.AddHostFilter(typeof(HappyHanukkahHostFilter));
}

Running Host Filters at Host Startup

Below is a typical ASP.NET Core Program.cs file. You can see RunAsync() has been replaced with RunWithFiltersAsync().

That is all that is required to run the host filters.

public class Program
{
    public static async Task Main(string[] args)
    {
        //await CreateHostBuilder(args).Build().RunAsync();
        await CreateHostBuilder(args).Build().RunWithFiltersAsync();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

Create a Host Filter Examples

Synchronous example using HostFilter base class

public class HoldTheBeerHostFilter : HostFilter
{
    protected override async Task Handle(IServiceProvider services)
    {
        ILogger logger = services.GetService<ILogger<HoldTheBeerHostFilter>>();
        logger.LogInformation("Hold my beer!");
    }
}

Asynchronous example using AsyncHostFilter base class

public class GetHeadlinesHostFilter : AsyncHostFilter
{
    protected override async Task Handle(IServiceProvider services, CancellationToken cancellationToken)
    {
        await new SuperAmazesauceOperationAsync().JustDoItAsync(cancellationToken);
    }
}

Direct Interface Implementation example

public class BeatItHostFilter : IHostFilter
{
    public async Task Handle(IServiceProvider services, CancellationToken cancellationToken)
    {
        await new JustBeatItAsync().GimmeAHeeeHeeeAsync(cancellationToken);
    }
}

Samples

See https://github.com/TheDanielDoyle/HostFilters/tree/develop/Samples for .NET Core or ASP.NET Core examples.

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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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

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 70 6/17/2024
7.0.0 345 11/28/2022
6.0.0 308 11/28/2022
5.0.0 300 11/28/2022
2.0.4 354 2/12/2021
2.0.3 420 6/27/2020
2.0.2 427 6/23/2020
2.0.1 474 12/17/2019
2.0.0 453 12/12/2019