DEV Community

Cover image for Building Better .NET Worker Services with Cursor Rules
burak
burak

Posted on

Building Better .NET Worker Services with Cursor Rules

πŸš€ Recently, I started building my own Cursor rule set for creating .NET Core Background Worker projects.

The goal is simple:
Teach AI to generate not just β€œworking code”, but production-ready architecture from the start.

Now, every new Worker Service automatically includes:
βœ… Clean Architecture
βœ… BackgroundService pattern
βœ… Serilog logging
βœ… Docker support
βœ… HealthChecks
βœ… IOptions pattern
βœ… Graceful shutdown
βœ… Retry policies with Polly

One rule that made a huge difference πŸ‘‡

Do not consider the project complete unless Dockerfile and .dockerignore are created.

This small convention changes the output from:
❌ β€œAI-generated demo project”
to
βœ… deployable backend infrastructure.

I think the real power of AI coding is not prompt engineering β€”
it’s teaching engineering conventions to AI.

Cursor Rules and ChatGPT Skills are becoming incredibly powerful for standardizing software development workflows.

Here is the rule.

`---
description: "Use this rule when creating or modifying .NET 8 Background Worker / Worker Service projects."
globs:

  • "*/.cs"
  • "*/.csproj" alwaysApply: false ---

.NET 8 Background Worker Standards

You are generating a production-ready .NET 8 Worker Service project.

Architecture

Use:

  • Clean Architecture
  • SOLID principles
  • Dependency Injection
  • Options Pattern
  • Async/Await best practices

Project structure:

src/
Worker/
Application/
Domain/
Infrastructure/

tests/
Worker.Tests/

Worker Rules

  • Use BackgroundService
  • Keep business logic outside Worker.cs
  • Use interfaces for external services
  • Support CancellationToken everywhere
  • Add structured logging
  • Handle graceful shutdown correctly
  • Add retry policies with Polly for external calls
  • Use IConfiguration + IOptions

Logging

Use:

  • Serilog
  • Console logging
  • Enrichers when necessary

Add logs for:

  • Startup
  • Shutdown
  • Errors
  • Retry attempts
  • Successful operations

Docker

Every generated project MUST include:

  • Dockerfile
  • .dockerignore
  • Multi-stage Docker build
  • .NET 8 official images
  • Production-ready container setup

Example requirements:

  • Use sdk image for build
  • Use runtime image for final stage
  • Minimize image size
  • Copy only required files

Do not consider the project complete unless Dockerfile and .dockerignore are created.

Configuration

Include:

  • appsettings.json
  • appsettings.Development.json
  • Environment variable support

README

Generate a README containing:

  • Project overview
  • Run instructions
  • Docker build/run commands
  • Environment variables
  • Architecture overview

Code Quality

Prefer:

  • Production-ready examples
  • Minimal but scalable structure
  • Readable naming
  • Small focused classes

Avoid:

  • Overengineering
  • Business logic in infrastructure
  • Static helper abuse
  • Blocking calls`

Thank you.

Top comments (0)