Skip to content

CenterEdge/Shawarma.AspNetCore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Shawarma.AspNetCore

Build Status Nuget (with prereleases)

ASP.NET Core middleware and service hosting library designed to interact with Shawarma. This allows background services, implemented similarly to IHostedService, to be started and stopped based on whether the application instance is live on a Kubernetes load balancer. This assists with blue/green deployments to Kubernetes, and ensures that old application instances stop background processing of things like message queues.

For more details about Shawarma, see https://github.com/CenterEdge/shawarma.

Usage

// This is an example in Program.cs using the Minimal API approach
using Shawarma.AspNetCore;
using Shawarma.AspNetCore.Hosting;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddShawarmaHosting()
    .AddShawarmaService<TestService>();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}

app.MapShawarma();
app.MapGet("/", () => "Hello World!");

await app.RunAsync();
// Use this approach in Startup.cs if you are not using the Minimal API approach
public void ConfigureServices(IServiceCollection services)
{
    services
        .AddRouting()
        .AddShawarmaHosting()
        // Add any IShawarmaService instances to be managed
        .AddShawarmaService<TestService>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapShawarma();

        endpoints.MapGet("", async context =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    });
}
public class TestService : GenericShawarmaService
{
    public TestService(ILogger<TestService> logger)
        : base(logger)
    {
    }

    protected override Task StartInternalAsync(CancellationToken cancellationToken)
    {
        // Start doing work here
        return Task.CompletedTask;
    }

    protected override Task StopInternalAsync(CancellationToken cancellationToken)
    {
        // Stop doing work here
        return Task.CompletedTask;
    }
}

Older .NET Core Versions

For applications not using endpoint routing, middleware may be used instead. However, this approach is considered obsolete.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    // Add before MVC or other handlers
    app.UseShawarma();

    app.Run(async (context) =>
    {
        await context.Response.WriteAsync("Hello World!");
    });
}

Developing

See Developing Shawarma.AspNetCore for instructions for development.