A robust asynchronous HTTP task processing service built with .NET 9. This service allows users to schedule HTTP requests to be executed in the background, track their status, and manage their lifecycle.
- .NET 9 (Web API, Worker Service)
- Entity Framework Core (PostgreSQL)
- xUnit, Moq, FluentAssertions (Testing)
- Microsoft.Extensions.Logging (Structured Logging)
- Asynchronous Processing: Tasks are queued and processed in the background without blocking the API.
- Status Tracking: Real-time tracking of task lifecycle:
Pending->Running->Completed/Failed/Cancelled. - Retry Logic: Automatic retries for failed tasks with exponential backoff (default: 3 retries with 5s, 10s, 20s delays).
- Cancellation: Ability to cancel pending or running tasks.
- Robust Error Handling: Comprehensive logging and exception handling to prevent data loss.
- .NET 9 SDK
- PostgreSQL Database
Update appsettings.json in src/HttpTaskService.Api with your database connection string:
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Port=5432;Database=HttpTaskDb;Username=postgres;Password=yourpassword"
}-
Apply Migrations:
dotnet ef database update --project src/HttpTaskService.Infrastructure --startup-project src/HttpTaskService.Api
-
Start the API:
dotnet run --project src/HttpTaskService.Api
The API will be available at
http://localhost:5216.
Schedule a new HTTP task.
- Endpoint:
POST /api/tasks - Body:
{ "url": "https://example.com" } - Response:
201 Created{ "taskId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "status": "pending" }
Retrieve the current status and result of a task.
- Endpoint:
GET /api/tasks/{id} - Response:
200 OK{ "taskId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "status": "completed", "result": { "url": "https://example.com", "statusCode": 200, "length": 1234, "durationMs": 150, "completedAt": "2023-10-01T12:00:00Z" } }
Cancel a pending or running task.
- Endpoint:
PATCH /api/tasks/{id}/cancel - Response:
200 OK{ "taskId": "3fa85f64-5717-4562-b3fc-2c963f66afa6", "status": "cancelled" }
The project includes a comprehensive unit test suite covering all handlers and business logic.
Run tests using the .NET CLI:
dotnet test src/HttpTaskService.Tests/HttpTaskService.Tests.csproj