Messaging

codewithmukesh/dotnet-claude-kit/skills/messaging

作者 codewithmukesh23300897f4d1无许可证754 个星标收录于 2026年10月8日更新于 2026年10月8日仓库2个月前更新

Asynchronous messaging patterns for .NET applications. Covers Wolverine and MassTransit, outbox pattern, saga and choreography, and broker configuration for RabbitMQ and Azure Service Bus. Load this skill when implementing event-driven communication, background processing, module-to-module messaging, or when the user mentions "Wolverine", "MassTransit", "message bus", "RabbitMQ", "Azure Service Bus", "event", "publish", "consumer", "outbox", "saga", "integration event", "queue", or "pub/sub".

AI 生成的概览

面向 .NET 应用的异步消息传递指南,涵盖 Wolverine、MassTransit、发件箱、Saga 与消息代理配置。

功能
该技能为 .NET 应用中的异步消息传递提供参考模式与代码示例。内容涵盖 Wolverine 与 MassTransit 的配置、事件的发布与消费、事务性发件箱模式、Saga 与编排的取舍,以及 RabbitMQ 和 Azure Service Bus 的代理配置。它还列出反模式并给出方案选择的决策指南。该技能仅为说明性内容,不生成文件或脚本。
适用场景
适用于在 .NET 中实现事件驱动通信、后台处理或模块间消息传递的场景。当涉及 Wolverine、MassTransit、RabbitMQ、Azure Service Bus、发件箱、Saga、队列或发布订阅时也适用。
运行要求
不含脚本或附带资源,除智能体本身外无需其他工具。其指导假定存在使用 Wolverine 或 MassTransit 的 .NET 项目、EF Core,以及 RabbitMQ 或 Azure Service Bus 等消息代理,并需要这些服务所需的凭据和网络访问。

Messaging

Core Principles

  1. Wolverine is the recommended default — MIT licensed, combines mediator + messaging in one library with built-in outbox, saga support, and convention-based handlers. MassTransit is an alternative but requires a commercial license from v9.
  2. Outbox pattern for reliability — Always use the transactional outbox to ensure messages are published only when the database transaction succeeds.
  3. Choreography for simple flows, saga for complex — If a workflow has 2-3 steps, use event choreography. If it has compensating actions or complex state, use a saga.
  4. Messages are contracts — Put message types in a shared contracts project. Keep them as simple records with primitive types.

Patterns

Wolverine Setup

csharp
// Program.csbuilder.Host.UseWolverine(opts =>{    // Auto-discover handlers from this assembly    opts.Discovery.IncludeAssembly(typeof(Program).Assembly);
    // RabbitMQ transport    opts.UseRabbitMq(rabbit =>    {        rabbit.HostName = "localhost";        // Or from configuration:        // rabbit.HostName = builder.Configuration["RabbitMq:Host"]!;    })    .AutoProvision()   // Create queues/exchanges automatically    .AutoPurgeOnStartup(); // Dev only — clear queues on startup
    // Enable transactional outbox with EF Core    opts.Services.AddDbContextWithWolverineIntegration<AppDbContext>(x =>        x.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
    opts.Policies.AutoApplyTransactions(); // Wrap handlers in DB transactions});

Why: UseWolverine() registers handler discovery, transport, and outbox in one place. AutoProvision() eliminates manual broker setup during development.

Publishing Events

Wolverine supports two publishing styles: cascading messages (return values) and explicit publishing.

csharp
// Message contract (in shared Contracts project)public record OrderCreated(Guid OrderId, string CustomerId, decimal Total, DateTimeOffset CreatedAt);
// Style 1: Cascading messages — return the event from the handler// Wolverine automatically publishes returned messages after the handler completes.public static class CreateOrder{    public record Command(string CustomerId, List<OrderItem> Items);    public record Response(Guid OrderId, decimal Total);
    public static async Task<(Response, OrderCreated)> HandleAsync(        Command command, AppDbContext db, TimeProvider clock, CancellationToken ct)    {        var order = Order.Create(command.CustomerId, command.Items, clock.GetUtcNow());        db.Orders.Add(order);        await db.SaveChangesAsync(ct);
        var response = new Response(order.Id, order.Total);        var @event = new OrderCreated(order.Id, order.CustomerId, order.Total, order.CreatedAt);
        return (response, @event); // Both are published automatically    }}
csharp
// Style 2: Explicit publishing via IMessageBuspublic static class CreateOrder{    public record Command(string CustomerId, List<OrderItem> Items);    public record Response(Guid OrderId, decimal Total);
    public static async Task<Response> HandleAsync(        Command command, AppDbContext db, IMessageBus bus, TimeProvider clock, CancellationToken ct)    {        var order = Order.Create(command.CustomerId, command.Items, clock.GetUtcNow());        db.Orders.Add(order);        await db.SaveChangesAsync(ct);
        await bus.PublishAsync(new OrderCreated(            order.Id, order.CustomerId, order.Total, order.CreatedAt));
        return new Response(order.Id, order.Total);    }}

Why: Cascading messages (tuple return) are simpler and testable — the handler is a pure function. Use explicit IMessageBus when publishing is conditional or requires multiple events.

Consuming Events

Wolverine uses convention-based handlers — no interface, no base class. Just a Handle method with the message type as the first parameter.

csharp
// Notifications module — handles OrderCreated from Orders modulepublic static class OrderCreatedHandler{    public static async Task HandleAsync(        OrderCreated message, NotificationsDbContext db, ILogger logger, CancellationToken ct)    {        logger.LogInformation("Processing OrderCreated: {OrderId}", message.OrderId);
        var notification = new OrderNotification(message.OrderId, message.CustomerId);        db.Notifications.Add(notification);        await db.SaveChangesAsync(ct);    }}

Why: Convention-based handlers have zero ceremony. Wolverine discovers them by signature: any public method named Handle/HandleAsync/Consume/ConsumeAsync with the message type as the first parameter.

Transactional Outbox

Ensures messages are only published if the database transaction succeeds.

csharp
// 1. Register DbContext with Wolverine integrationbuilder.Host.UseWolverine(opts =>{    opts.Services.AddDbContextWithWolverineIntegration<AppDbContext>(x =>        x.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
    opts.Policies.AutoApplyTransactions();});
// 2. DbContext — add Wolverine outbox tablespublic class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options){    public DbSet<Order> Orders => Set<Order>();
    protected override void OnModelCreating(ModelBuilder modelBuilder)    {        // Wolverine inbox/outbox tables — required for transactional messaging        modelBuilder.AddIncomingWolverineMessageTable();        modelBuilder.AddOutgoingWolverineMessageTable();    }}

Why: AddDbContextWithWolverineIntegration + AutoApplyTransactions wraps every handler in a transaction that includes outbox writes. Messages are only sent after the transaction commits — no dual-write problem.

Saga (Stateful Orchestration)

Wolverine sagas use a Saga<T> base class with Start and Handle methods. Cascading messages drive the saga forward.

csharp
public record OrderSagaState(Guid Id){    public string? CustomerId { get; set; }    public bool PaymentReceived { get; set; }}
public class OrderSaga : Saga<OrderSagaState>{    public Guid Id { get; set; }
    // Start the saga when an OrderCreated event arrives    public static (OrderSagaState, ProcessPayment) Start(OrderCreated message)    {        var state = new OrderSagaState(message.OrderId)        {            CustomerId = message.CustomerId        };
        var command = new ProcessPayment(message.OrderId, message.Total);        return (state, command); // State is persisted, command is sent    }
    // Handle payment result    public CompleteOrder Handle(PaymentCompleted message)    {        PaymentReceived = true;        MarkCompleted(); // Ends the saga        return new CompleteOrder(Id);    }
    // Compensating action on failure    public CancelOrder Handle(PaymentFailed message)    {        MarkCompleted();        return new CancelOrder(Id);    }}

Why: Wolverine sagas use simple C# methods instead of a state machine DSL. Each handler returns cascading messages to drive the workflow. MarkCompleted() cleans up the saga state.

Alternative: MassTransit

MassTransit is a mature alternative with a commercial license requirement from v9+. Key API surface:

csharp
// Setupbuilder.Services.AddMassTransit(x =>{    x.SetKebabCaseEndpointNameFormatter();    x.AddConsumers(typeof(Program).Assembly);    x.UsingRabbitMq((context, cfg) =>    {        cfg.Host(builder.Configuration.GetConnectionString("RabbitMq"));        cfg.ConfigureEndpoints(context);    });});
// Publishingawait publishEndpoint.Publish(new OrderCreated(...), ct);
// Consuming — requires IConsumer<T> interfacepublic class OrderCreatedConsumer(AppDbContext db) : IConsumer<OrderCreated>{    public async Task Consume(ConsumeContext<OrderCreated> context)    {        var message = context.Message;        // Handle event...    }}
// Outboxx.AddEntityFrameworkOutbox<AppDbContext>(o =>{    o.UsePostgres();    o.UseBusOutbox();});
// Saga — uses MassTransitStateMachine<TState>public class OrderSaga : MassTransitStateMachine<OrderSagaState> { /* ... */ }

License note: MassTransit v9+ requires a commercial license for production use. Wolverine (MIT) is the recommended default for new projects.

Anti-patterns

Don't Publish Events Without Outbox

csharp
// BAD — if SaveChanges succeeds but Publish fails, data is inconsistentawait db.SaveChangesAsync(ct);await bus.PublishAsync(new OrderCreated(...));
// GOOD — use transactional outbox (messages are in the same transaction)// Configure AddDbContextWithWolverineIntegration() + AutoApplyTransactions()// Wolverine handles this automatically

Don't Put Complex Logic in Message Contracts

csharp
// BAD — behavior in a messagepublic record OrderCreated(Guid OrderId){    public decimal CalculateShipping() => /* logic */; // DON'T}
// GOOD — messages are pure datapublic record OrderCreated(Guid OrderId, string CustomerId, decimal Total, DateTimeOffset CreatedAt);

Don't Use Fire-and-Forget for Important Events

csharp
// BAD — no guarantee of delivery_ = Task.Run(() => bus.PublishAsync(new OrderCreated(...)));
// GOOD — await the publish (with outbox, this is transactional)await bus.PublishAsync(new OrderCreated(...));

Decision Guide

ScenarioRecommendation
Module-to-module communication (new project)Wolverine with events (MIT, free)
Module-to-module communication (existing MassTransit)MassTransit (commercial license required from v9)
Reliable event publishingTransactional outbox (both Wolverine and MassTransit support this)
Simple 2-3 step workflowEvent choreography
Complex workflow with compensationWolverine saga or MassTransit saga
Local development brokerRabbitMQ (via Docker or Aspire)
Production cloud brokerAzure Service Bus or RabbitMQ
Want single lib for mediator + messagingWolverine (replaces both Mediator and MassTransit)

来源与署名

来源:codewithmukesh/dotnet-claude-kit位于skills/messaging提交2330089

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架

更多来自 codewithmukesh/dotnet-claude-kit 的技能

Wrap Up

codewithmukesh

在会话结束时把已完成工作、待办任务与经验写入交接文件,并在会话开始时重新载入。

Productivity & Workflow7542个月前更新

Workflow Mastery

codewithmukesh

Claude Code workflow mastery for .NET developers. Covers parallel execution with git worktrees, plan mode strategy, verification loops, auto-formatting hooks, permission setup for dotnet CLI, prompting techniques, subagent patterns, and context discipline — token budget management, MCP-first navigation, lazy loading, and subagent isolation — all adapted for the .NET ecosystem. Load this skill when setting up Claude Code for a .NET project, optimizing workflows, running parallel sessions, when context is running low or sessions feel sluggish, when exploring a large codebase efficiently, or when the user mentions "productivity", "workflow", "parallel", "worktree", "plan mode", "permissions", "hooks", "10x", "setup Claude Code", "speed up development", "context", "tokens", "budget", "running out of context", "too many files", or "large codebase". Inspired by tips from Boris Cherny (creator of Claude Code) and the Anthropic team.

待分类7542个月前更新

Vertical Slice

codewithmukesh

指导 .NET 开发者采用垂直切片架构组织应用,涵盖功能文件夹、端点分组与处理程序模式。

Software Development7542个月前更新

Testing

codewithmukesh

Testing strategy for .NET 10 applications. Covers xUnit v3, WebApplicationFactory for integration tests, Testcontainers for real database testing, Verify for snapshot testing, and the AAA pattern. Load this skill when writing tests, setting up test infrastructure, reviewing test coverage, or when the user mentions "test", "xUnit", "WebApplicationFactory", "Testcontainers", "integration test", "unit test", "bUnit", "snapshot test", "Verify", "test coverage", "AAA pattern", "WireMock", or "FakeTimeProvider".

待分类7542个月前更新

Tdd

codewithmukesh

Guided test-driven development workflow for .NET 10 using xUnit v3, WebApplicationFactory, Testcontainers, and Verify snapshots. Follows the strict red-green-refactor cycle. Use when: "TDD", "test-driven", "let's TDD this", "red green refactor", "write the test first", or when building a feature with clear acceptance criteria.

待分类7542个月前更新

Spec

codewithmukesh

通过结构化提问,把模糊的功能想法转化为双方确认并持久化的规格说明文件。

Productivity & Workflow7542个月前更新