TheDanielDoyle.HostFilters
2.0.1
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
<PackageReference Include="TheDanielDoyle.HostFilters" Version="2.0.1" />
paket add TheDanielDoyle.HostFilters --version 2.0.1
#r "nuget: TheDanielDoyle.HostFilters, 2.0.1"
// 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
- Implement IHostFilter in a class, or derive from the built-in HostFilter or AsyncHostFilter base classes.
- Register all concrete types implementing IHostFilter in dependency injection.
- Replace RunAsync() with RunWithFiltersAsync() in your Program.cs Main method.
- 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 | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.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. |
-
.NETStandard 2.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.