Skip to main content

.NET Microservices with MassTransit

Build .NET microservices with MassTransit — message contracts, sagas, state machines, and reliable messaging with RabbitMQ or Azure.

Vul de plaatshouders in

Edit the values, then copy your finished prompt.

Jouw Prompt
prompt.txt

                                

What this prompt does

This prompt designs a microservices communication layer using MassTransit in [dotnet_version] with [message_broker]. It defines [contract_count] message types — commands, events, and request/response — in a shared contracts library with versioning, builds consumers for [consumer_scenarios] with a [retry_strategy] and dead-lettering, implements a saga state machine for [saga_scenario], configures the transactional outbox with [outbox_storage], sets up exchange and queue topology for [topology_requirements], and integrates distributed tracing via [telemetry_tool].

The structure works because a system that outgrows direct service calls needs messaging that fails cleanly rather than corrupting state. The saga defines states, transitions, and compensating actions so a failed step rolls back instead of leaving an order half-processed, and the outbox pattern guarantees delivery even when the broker is briefly unavailable. The MassTransit test harness and [telemetry_tool] tracing are what keep distributed flows debuggable when something goes wrong across service boundaries.

When to use it

  • A system has outgrown direct service calls and needs reliable, decoupled messaging
  • You are defining [contract_count] message contracts and need a versioning strategy in a shared library
  • You need consumers for [consumer_scenarios] with a clear [retry_strategy] and dead-letter handling
  • A multi-step process like [saga_scenario] needs a saga with compensating actions on failure
  • You want the transactional outbox with [outbox_storage] so messages aren't lost if the broker is down
  • You need distributed tracing across service boundaries via [telemetry_tool]

Example output

Expect a design with code: a shared contracts library defining commands, events, and request/response messages with versioning, consumers with retry and dead-letter configuration, a saga state machine with states, transitions, and compensating actions, transactional-outbox setup, exchange and queue topology with naming conventions and bindings, test-harness examples for consumers and sagas, distributed-tracing integration, and containerized deployment configuration with health checks and graceful shutdown.

Pro tips

  • Version your [contract_count] message contracts from the start, since changing a published message shape later without versioning breaks every consumer that depends on it
  • Tune [retry_strategy] to distinguish transient from permanent failures — immediate retries then exponential backoff handles blips, but a poison message should reach the error queue, not retry forever
  • Design saga compensating actions for [saga_scenario] deliberately; a failed payment must release reserved inventory, or you leak state that's painful to reconcile
  • Enable the transactional outbox with [outbox_storage] so a message and its database change commit together, avoiding the lost-message gap if the broker is briefly down
  • Use the MassTransit test harness to assert message flow without a live broker, which keeps saga and consumer tests fast and deterministic
  • Add [telemetry_tool] tracing early, because correlating a request across several services after the fact is far harder than instrumenting it up front

Frequently Asked Questions

What problem does the saga state machine solve?
It coordinates a multi-step process like `[saga_scenario]` across services, tracking state and defining compensating actions for failures. If a later step fails — say payment after inventory is reserved — the saga triggers compensation to release that inventory, so the overall transaction rolls back cleanly instead of leaving the system in a half-completed, inconsistent state.
Why do I need the outbox pattern with MassTransit?
The transactional outbox with `[outbox_storage]` ensures a message and the database change that produced it commit together atomically. Without it, your service could update the database but fail to publish the message if the broker is briefly unavailable, leaving downstream services unaware of a change that already happened. The outbox closes that gap.
Can I test consumers and sagas without a running broker?
Yes. The prompt uses the MassTransit test harness, which lets you assert that messages are consumed, sagas transition correctly, and the expected messages are published, all in-memory. This keeps consumer and saga tests fast and deterministic, while you mock external dependencies to isolate the message flow from real downstream calls.
Does this work with brokers other than RabbitMQ?
Yes. Set `[message_broker]` to your actual transport, such as RabbitMQ or Azure Service Bus, and MassTransit abstracts much of the messaging code so it stays largely consistent. Some topology and feature details differ between transports, so confirm that capabilities like your chosen `[topology_requirements]` are supported by the specific broker you deploy.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

Meer in C# & .NET Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support