Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

joncloud/amp-net

Repository files navigation

Amp.NET

Travis NuGet

amp.net

Deprecation

This project is no longer maintained and has been made obsolete by improvements to the ASP.NET Core platform. Consider building your warm up routines in your main entry point, i.e., Program.cs. For example:

class Program {
  static void WarmUp(ApplicationDbContext context) {
    context.Users.FirstOrDefault();
  }

  static void Main(string[] args) {
    var host = CreateHostBuilder(args).Build();

    WarmUp(
      host.Services
        .GetRequiredService<ApplicationDbContext>()
    );

    host.Run();
  }
}

Description

Amp.NET provides warm up routines to ASP.NET Core applications.

Licensing

Released under the MIT License. See the LICENSE file for further details.

Installation

In the Package Manager Console execute

Install-Package Amp

Or update *.csproj to include a dependency on

<ItemGroup>
  <PackageReference Include="Amp" Version="0.1.0-*" />
</ItemGroup>

Usage

You can configure your application in order to specify Amp's behavior.

  • Path - ("/warmup") The path which invokes the asynchronous initialization
  • Parallelism - (WarmUpParallelism.Parallel) Execution flow for each individual IWarmUp.
public void ConfigureServices(IServiceCollection services) => 
  services.AddWarmUp(builder => {
    builder.Path = "/my-custom-path";
    builder.Parallelism = WarmUpParallelism.Sequential;
  });

You can warm up your application in one of two ways: asynchronous or synchronous.

Asynchronous

Use asynchronous warm up when you want to initialize the system separately from application boot up.

public void Configure(IApplicationBuilder app, IHostingEnvironment env) =>
  app.UseWarmUp().UseMvc();  

Synchronous

Use synchronous warm up when you need to initialize the system before the application begins to take requests.

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
  app.WarmUpAsync().GetAwaiter().GetResult();
  app.UseMvc();
}

Code Samples

Sample integration with Entity Framework Core:

class Startup {
  ...
  
  public void ConfigureServices(IServiceCollection services) => 
    services.AddWarmUp().AddScoped<IWarmUp, ApplicationDbContextWarmUp>();
    
  public void Configure(IApplicationBuilder app, IHostingEnvironment env) =>
    app.UseWarmUp().UseMvc();
}

class ApplicationDbContextWarmUp : IWarmUp {
  readonly ApplicationDbContext _context;
  public ApplicationDbContextWarmUp(ApplicationDbContext context) => _context = context;
  
  public Task InvokeAsync() => _context.Users.AnyAsync();
}

For additional usage see Tests.