https://github.com/centeredge/shawarma.aspnetcore
ASP.Net Core Middleware For Integrating With Shawarma
https://github.com/centeredge/shawarma.aspnetcore
Last synced: about 1 year ago
JSON representation
ASP.Net Core Middleware For Integrating With Shawarma
- Host: GitHub
- URL: https://github.com/centeredge/shawarma.aspnetcore
- Owner: CenterEdge
- License: apache-2.0
- Created: 2019-06-24T14:15:40.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2022-12-09T20:15:22.000Z (over 3 years ago)
- Last Synced: 2025-02-15T23:41:56.341Z (over 1 year ago)
- Language: C#
- Size: 67.4 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Shawarma.AspNetCore
[](https://travis-ci.org/CenterEdge/Shawarma.AspNetCore)
[](https://www.nuget.org/packages/Shawarma.AspNetCore)
ASP.NET Core middleware and service hosting library designed to interact with Shawarma. This allows
background services, implemented similarly to [IHostedService](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.2&tabs=visual-studio),
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 .
## Usage
```cs
// 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();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.MapShawarma();
app.MapGet("/", () => "Hello World!");
await app.RunAsync();
```
```cs
// 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();
}
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!");
});
});
}
```
```cs
public class TestService : GenericShawarmaService
{
public TestService(ILogger 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.
```cs
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](./Developing.md) for instructions for development.