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 (1)

Collapse
 
agentic_architect profile image
Agentic Architect

Thanks for posting the full rule, this is really useful. The Dockerfile gate is the kind of small line that stops Cursor from handing you something that compiles but never actually deploys โ€” I've seen that exact failure on a Worker repo before I started treating "done" as a convention, not a vibe. One thing that's helped me since: keep alwaysApply: false with tight globs so you're not getting BackgroundService + Polly boilerplate every time you touch a shared DTO in another project in the same solution.