-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Targets net6.0 and above, with full Dependency Injection support with per queue item scope.
- Loading branch information
Darran
committed
Sep 5, 2024
1 parent
7ec970d
commit d261b0a
Showing
21 changed files
with
440 additions
and
199 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...ng.BackgroundQueue.Examples.WebApi/DalSoft.Hosting.BackgroundQueue.Examples.WebApi.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<RootNamespace>DalSoft.Hosting.BackgroundQueue.Examples.WebApi</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8"/> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.3"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DalSoft.Hosting.BackgroundQueue\DalSoft.Hosting.BackgroundQueue.csproj" /> | ||
</ItemGroup> | ||
</Project> |
6 changes: 6 additions & 0 deletions
6
....Hosting.BackgroundQueue.Examples.WebApi/DalSoft.Hosting.BackgroundQueue.Test.WebApi.http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@DalSoft.Hosting.BackgroundQueue.Test.WebApi_HostAddress = http://localhost:5184 | ||
|
||
GET {{DalSoft.Hosting.BackgroundQueue.Test.WebApi_HostAddress}}/weatherforecast/ | ||
Accept: application/json | ||
|
||
### |
8 changes: 8 additions & 0 deletions
8
DalSoft.Hosting.BackgroundQueue.Examples.WebApi/Data/Entities/Person.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace DalSoft.Hosting.BackgroundQueue.Examples.WebApi.Data.Entities; | ||
|
||
public class Person | ||
{ | ||
public long PersonId { get; set; } | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
} |
10 changes: 10 additions & 0 deletions
10
DalSoft.Hosting.BackgroundQueue.Examples.WebApi/Data/PersonDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using DalSoft.Hosting.BackgroundQueue.Examples.WebApi.Data.Entities; | ||
|
||
namespace DalSoft.Hosting.BackgroundQueue.Examples.WebApi.Data; | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class PersonDbContext(DbContextOptions<PersonDbContext> options) : DbContext(options) | ||
{ | ||
public DbSet<Person> People { get; set; } | ||
} |
54 changes: 54 additions & 0 deletions
54
DalSoft.Hosting.BackgroundQueue.Examples.WebApi/Endpoints/TestBackgroundQueueEndpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using DalSoft.Hosting.BackgroundQueue.Examples.WebApi.Data; | ||
using DalSoft.Hosting.BackgroundQueue.Examples.WebApi.Data.Entities; | ||
|
||
namespace DalSoft.Hosting.BackgroundQueue.Examples.WebApi.Endpoints; | ||
|
||
public static class TestBackgroundQueueEndpoint | ||
{ | ||
public static void MapWeatherEndpoints(this WebApplication app) | ||
{ | ||
app.MapPost("/backgroundtasks", (IBackgroundQueue backgroundQueue) => | ||
{ | ||
// queue background tasks | ||
for (var i = 0; i < 20; i++) | ||
{ | ||
backgroundQueue.Enqueue(async ct => | ||
{ | ||
await Task.Delay(60000, ct); | ||
}); | ||
} | ||
return new { added = true }; | ||
}) | ||
.WithName("Add background Tasks") | ||
.WithOpenApi(); | ||
|
||
// Also test injecting BackgroundQueue instead of IBackgroundQueue | ||
app.MapGet("/backgroundtasks", (BackgroundQueue backgroundQueue) => new { backgroundQueue.Count, backgroundQueue.ConcurrentCount }) | ||
.WithName("Get Background Tasks") | ||
.WithOpenApi(); | ||
|
||
app.MapPost("/backgroundtasks/dependencyinjection", (IBackgroundQueue backgroundQueue) => | ||
{ | ||
backgroundQueue.Enqueue(async (ct, serviceScope) => | ||
{ | ||
await Task.Delay(5000, ct); | ||
var dbContext = serviceScope.ServiceProvider.GetRequiredService<PersonDbContext>(); | ||
await dbContext.People.AddAsync(new Person | ||
{ | ||
FirstName = $"FirstName {Guid.NewGuid()}", | ||
LastName = $"LastName {Guid.NewGuid()}" | ||
}, ct); | ||
await dbContext.SaveChangesAsync(ct); | ||
}); | ||
}) | ||
.WithName("Test Dependency Injection") | ||
.WithOpenApi(); | ||
|
||
app.MapPost("/backgroundtasks/exception", (IBackgroundQueue backgroundQueue) => | ||
{ | ||
backgroundQueue.Enqueue(async ct => throw new Exception("Testing exceptions")); | ||
}) | ||
.WithName("Test Exceptions Handling") | ||
.WithOpenApi(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
DalSoft.Hosting.BackgroundQueue.Examples.WebApi/Extensions/HostExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using DalSoft.Hosting.BackgroundQueue.Examples.WebApi.Data; | ||
|
||
namespace DalSoft.Hosting.BackgroundQueue.Examples.WebApi.Extensions; | ||
|
||
public static class HostExtensions | ||
{ | ||
public static void CreateDbIfNotExists(this IHost host) | ||
{ | ||
using var scope = host.Services.CreateScope(); | ||
var services = scope.ServiceProvider; | ||
try | ||
{ | ||
var context = services.GetRequiredService<PersonDbContext>(); | ||
context.Database.EnsureCreated(); | ||
|
||
} | ||
catch (Exception ex) | ||
{ | ||
var logger = services.GetRequiredService<ILogger<Program>>(); | ||
logger.LogError(ex, "An error occurred creating the DB."); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
DalSoft.Hosting.BackgroundQueue.Examples.WebApi/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using DalSoft.Hosting.BackgroundQueue.Examples.WebApi.Data; | ||
using DalSoft.Hosting.BackgroundQueue.Examples.WebApi.Endpoints; | ||
using DalSoft.Hosting.BackgroundQueue.Examples.WebApi.Extensions; | ||
using DalSoft.Hosting.BackgroundQueue.Extensions.DependencyInjection; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
var configuration = builder.Configuration; | ||
builder.Logging.ClearProviders().AddConsole(); | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
builder.Services.AddDbContext<PersonDbContext>(options => | ||
options.UseSqlServer(configuration.GetConnectionString(nameof(PersonDbContext)))); | ||
|
||
builder.Services.AddBackgroundQueue((exception, serviceScope) => | ||
{ | ||
serviceScope.ServiceProvider.GetRequiredService<ILogger<Program>>() | ||
.Log(LogLevel.Error, exception, exception.Message); | ||
}, maxConcurrentCount: 10); | ||
|
||
var app = builder.Build(); | ||
|
||
app.CreateDbIfNotExists(); | ||
|
||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.MapWeatherEndpoints(); | ||
|
||
app.Run(); | ||
|
41 changes: 41 additions & 0 deletions
41
DalSoft.Hosting.BackgroundQueue.Examples.WebApi/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:57219", | ||
"sslPort": 44341 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "http://localhost:5184", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:7020;http://localhost:5184", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
DalSoft.Hosting.BackgroundQueue.Examples.WebApi/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
DalSoft.Hosting.BackgroundQueue.Examples.WebApi/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"ConnectionStrings": { | ||
"PersonDbContext": "Server=(localdb)\\mssqllocaldb;Database=BackgroundQueueExample;Trusted_Connection=True;MultipleActiveResultSets=true" | ||
}, | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
11 changes: 6 additions & 5 deletions
11
DalSoft.Hosting.BackgroundQueue.Test/DalSoft.Hosting.BackgroundQueue.Test.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.