DotNetCore.CAP 10.0.0-preview-280318356

CAP Logo

CAP                     中文

Docs & Dashboard AppVeyor NuGet NuGet Preview Member project of .NET Core Community GitHub license

CAP is a .NET library that provides a lightweight, easy-to-use, and efficient solution for distributed transactions and event bus integration.

When building SOA or Microservice-based systems, services often need to be integrated via events. However, simply using a message queue cannot guarantee reliability. CAP leverages a local message table, integrated with your current database, to solve exceptions that can occur during distributed system communications. This ensures that event messages are never lost.

You can also use CAP as a standalone EventBus. It offers a simplified approach to event publishing and subscribing without requiring you to inherit or implement any specific interfaces.

Key Features

  • Core Functionality

    • Distributed Transactions: Guarantees data consistency across microservices using a local message table (Outbox Pattern).
    • Event Bus: High-performance, lightweight event bus for decoupled communication.
    • Guaranteed Delivery: Ensures messages are never lost, with automatic retries for failed messages.
  • Advanced Messaging

    • Delayed Messages: Native support for publishing messages with a delay, without relying on message queue features.
    • Flexible Subscriptions: Supports attribute-based, wildcard (*, #), and partial topic subscriptions.
    • Consumer Groups & Fan-Out: Easily implement competing consumer or fan-out patterns for load balancing or broadcasting.
    • Parallel & Serial Processing: Configure consumers for high-throughput parallel processing or ordered sequential execution.
    • Backpressure Mechanism: Automatically manages processing speed to prevent memory overload (OOM) under high load.
  • Extensibility & Integration

    • Pluggable Architecture: Supports a wide range of message queues (RabbitMQ, Kafka, Azure Service Bus, etc.) and databases (SQL Server, PostgreSQL, MongoDB, etc.).
    • Heterogeneous Systems: Provides mechanisms to integrate with non-CAP or legacy systems.
    • Customizable Filters & Serialization: Intercept the processing pipeline with custom filters and support various serialization formats.
  • Monitoring & Observability

    • Real-time Dashboard: A built-in web dashboard to monitor messages, view status, and manually retry.
    • Service Discovery: Integrates with Consul and Kubernetes for node discovery in a distributed environment.
    • OpenTelemetry Support: Built-in instrumentation for distributed tracing, providing end-to-end visibility.

Architecture Overview

CAP Architecture

CAP implements the Outbox Pattern as described in the eShop on .NET ebook.

Getting Started

1. Installation

Install the main CAP package into your project using NuGet.

PM> Install-Package DotNetCore.CAP

Next, install the desired transport and storage providers.

Transports (Message Queues):

PM> Install-Package DotNetCore.CAP.Kafka
PM> Install-Package DotNetCore.CAP.RabbitMQ
PM> Install-Package DotNetCore.CAP.AzureServiceBus
PM> Install-Package DotNetCore.CAP.AmazonSQS
PM> Install-Package DotNetCore.CAP.NATS
PM> Install-Package DotNetCore.CAP.RedisStreams
PM> Install-Package DotNetCore.CAP.Pulsar

Storage (Databases):

The event log table will be integrated into the database you select.

PM> Install-Package DotNetCore.CAP.SqlServer
PM> Install-Package DotNetCore.CAP.MySql
PM> Install-Package DotNetCore.CAP.PostgreSql
PM> Install-Package DotNetCore.CAP.MongoDB     // Requires MongoDB 4.0+ cluster

2. Configuration

Configure CAP in your Startup.cs or Program.cs.

public void ConfigureServices(IServiceCollection services)
{
    // If you are using EF as the ORM
    services.AddDbContext<AppDbContext>(); 
    
    // If you are using MongoDB
    services.AddSingleton<IMongoClient>(new MongoClient("..."));

    services.AddCap(x =>
    {
        // Using Entity Framework
        // CAP can auto-discover the connection string
        x.UseEntityFramework<AppDbContext>();

        // Using ADO.NET
        x.UseSqlServer("Your ConnectionString");
        x.UseMySql("Your ConnectionString");
        x.UsePostgreSql("Your ConnectionString");

        // Using MongoDB (requires a 4.0+ cluster)
        x.UseMongoDB("Your ConnectionString");

        // Choose your message transport
        x.UseRabbitMQ("HostName");
        x.UseKafka("ConnectionString");
        x.UseAzureServiceBus("ConnectionString");
        x.UseAmazonSQS(options => { /* ... */ });
        x.UseNATS("ConnectionString");
        x.UsePulsar("ConnectionString");
        x.UseRedisStreams("ConnectionString");
    });
}

3. Publish Messages

Inject ICapPublisher into your controller or service to publish events. As of version 7.0, you can also publish delayed messages.

public class PublishController : Controller
{
    private readonly ICapPublisher _capBus;

    public PublishController(ICapPublisher capPublisher)
    {
        _capBus = capPublisher;
    }

    [Route("~/adonet/transaction")]
    public IActionResult AdonetWithTransaction()
    {
        using (var connection = new MySqlConnection(ConnectionString))
        {
            // Start a transaction with auto-commit enabled
            using (var transaction = connection.BeginTransaction(_capBus, autoCommit: true))
            {
                // Your business logic...
                _capBus.Publish("xxx.services.show.time", DateTime.Now);
            }
        }
        return Ok();
    }

    [Route("~/ef/transaction")]
    public IActionResult EntityFrameworkWithTransaction([FromServices] AppDbContext dbContext)
    {
        using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: true))
        {
            // Your business logic...
            _capBus.Publish("xxx.services.show.time", DateTime.Now);
        }
        return Ok();
    }

    [Route("~/publish/delay")]
    public async Task<IActionResult> PublishWithDelay()
    {
        // Publish a message with a 30-second delay
        await _capBus.PublishDelayAsync(TimeSpan.FromSeconds(30), "xxx.services.show.time", DateTime.Now);
        return Ok();
    }
}

4. Subscribe to Messages

In a Controller Action

Add the [CapSubscribe] attribute to a controller action to subscribe to a topic.

public class SubscriptionController : Controller
{
    [CapSubscribe("xxx.services.show.time")]
    public void CheckReceivedMessage(DateTime messageTime)
    {
        Console.WriteLine($"Message received: {messageTime}");
    }
}

In a Business Logic Service

If your subscriber is not in a controller, the class must implement the ICapSubscribe interface.

namespace BusinessCode.Service
{
    public interface ISubscriberService
    {
        void CheckReceivedMessage(DateTime datetime);
    }

    public class SubscriberService : ISubscriberService, ICapSubscribe
    {
        [CapSubscribe("xxx.services.show.time")]
        public void CheckReceivedMessage(DateTime datetime)
        {
            // Handle the message
        }
    }
}

Remember to register your service in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<ISubscriberService, SubscriberService>();

    services.AddCap(x =>
    {
        // ...
    });
}

Asynchronous Subscriptions

For async operations, your subscription method should return a Task and can accept a CancellationToken.

public class AsyncSubscriber : ICapSubscribe
{
    [CapSubscribe("topic.name")]
    public async Task ProcessAsync(Message message, CancellationToken cancellationToken)
    {
        await SomeOperationAsync(message, cancellationToken);
    }
}

Partial Topic Subscriptions

Group topic subscriptions by defining a partial topic on the class level. The final topic will be a combination of the class-level and method-level topics. In this example, the Create method subscribes to customers.create.

[CapSubscribe("customers")]
public class CustomersSubscriberService : ICapSubscribe
{
    [CapSubscribe("create", isPartial: true)]
    public void Create(Customer customer)
    {
        // ...
    }
}

Subscription Groups

Subscription groups are similar to consumer groups in Kafka. They allow you to load-balance message processing across multiple instances of a service.

By default, CAP uses the assembly name as the group name. If multiple subscribers in the same group subscribe to the same topic, only one will receive the message (competing consumers). If they are in different groups, all will receive the message (fan-out).

You can specify a group directly in the attribute:

[CapSubscribe("xxx.services.show.time", Group = "group1")]
public void ShowTime1(DateTime datetime)
{
    // ...
}

[CapSubscribe("xxx.services.show.time", Group = "group2")]
public void ShowTime2(DateTime datetime)
{
    // ...
}

You can also set a default group name in the configuration:

services.AddCap(x =>
{
    x.DefaultGroup = "my-default-group";  
});

Dashboard

CAP provides a real-time dashboard to view sent and received messages and their status.

PM> Install-Package DotNetCore.CAP.Dashboard

The dashboard is accessible by default at http://localhost:xxx/cap. You can customize the path via options: x.UseDashboard(opt => { opt.PathMatch = "/my-cap"; });.

For distributed environments, the dashboard supports service discovery to view data from all nodes.

Contribute

We welcome contributions! Participating in discussions, reporting issues, and submitting pull requests are all great ways to help. Please read our contributing guidelines (we can create this file if it doesn't exist) to get started.

License

CAP is licensed under the MIT License.

Showing the top 20 packages that depend on DotNetCore.CAP.

Packages Downloads
ZhonTai.Admin
中台Admin权限管理接口库
93
ZhonTai.Admin
中台Admin权限管理接口库
94
ZhonTai.Admin
中台Admin权限管理接口库
95
ZhonTai.Admin
中台Admin权限管理接口库
97
ZhonTai.Admin
中台Admin权限管理接口库
98
ZhonTai.Admin
中台Admin权限管理接口库
101
ZhonTai.Admin
中台Admin权限管理接口库
103
ZhonTai.Admin
中台Admin权限管理接口库
109
ZhonTai.Admin
中台Admin权限管理接口库
112
ZhonTai.Admin
中台Admin权限管理接口库
113
ZhonTai.Admin
中台Admin权限管理接口库
114
ZhonTai.Admin
中台Admin权限管理接口库
116
ZhonTai.Admin
中台Admin权限管理接口库
126
ZhonTai.Admin.Core
中台Admin权限管理核心库
93
ZhonTai.Admin.Core
中台Admin权限管理核心库
94
ZhonTai.Admin.Core
中台Admin权限管理核心库
96

Version Downloads Last updated
10.0.0 62 11/30/2025
10.0.0-preview-280318356 43 11/23/2025
8.4.1 40 10/24/2025
8.4.0 48 09/15/2025
8.4.0-preview-271338004 39 08/15/2025
8.4.0-preview-270476069 47 08/01/2025
8.4.0-preview-270285676 47 08/02/2025
8.3.5 51 05/28/2025
8.3.5-preview-262164550 48 04/26/2025
8.3.4 49 04/27/2025
8.3.3 58 03/05/2025
8.3.3-preview-255432523 54 02/09/2025
8.3.3-preview-254219859 61 01/22/2025
8.3.2 58 01/02/2025
8.3.2-preview-248100009 58 11/14/2024
8.3.1 61 11/14/2024
8.3.1-preview-247022046 55 12/10/2024
8.3.0 54 11/14/2024
8.3.0-preview-243613753 56 11/14/2024
8.2.0 55 11/14/2024
8.2.0-preview-234883029 58 11/14/2024
8.2.0-preview-233720681 54 11/28/2024
8.1.2 61 11/14/2024
8.1.1 66 04/27/2024
8.1.1-preview-230008876 53 11/14/2024
8.1.0 61 11/14/2024
8.1.0-preview-226548602 64 11/14/2024
8.1.0-preview-225165712 63 11/14/2024
8.0.1 58 02/04/2024
8.0.0 60 11/14/2024
8.0.0-preview-218723843 58 11/14/2024
8.0.0-preview-218688659 64 11/14/2024
7.2.3-preview-217562936 53 11/14/2024
7.2.3-preview-217174309 67 11/28/2024
7.2.3-preview-216527974 50 11/14/2024
7.2.2 62 11/14/2024
7.2.1 73 11/13/2024
7.2.0 65 11/14/2024
7.2.0-preview-207226127 63 11/14/2024
7.2.0-preview-207205557 52 11/14/2024
7.2.0-preview-204044155 67 11/14/2024
7.1.4 70 11/14/2024
7.1.3 68 06/16/2023
7.1.3-preview-200912175 59 11/14/2024
7.1.3-preview-200730887 53 11/14/2024
7.1.2 57 11/14/2024
7.1.2-preview-198398083 56 11/14/2024
7.1.1 62 11/14/2024
7.1.1-preview-197551401 61 11/14/2024
7.1.1-preview-196764828 59 11/14/2024
7.1.1-preview-196761499 57 11/28/2024
7.1.0 62 11/14/2024
7.1.0-preview-194230942 57 11/14/2024
7.1.0-preview-193202380 59 11/14/2024
7.0.3 55 11/27/2024
7.0.2 64 11/14/2024
7.0.2-preview-189692844 59 11/14/2024
7.0.1 59 11/14/2024
7.0.0 70 06/16/2023
7.0.0-preview-186133345 55 11/14/2024
7.0.0-preview-185881699 65 11/14/2024
7.0.0-preview-185533510 59 11/14/2024
7.0.0-preview-185469232 54 11/14/2024
7.0.0-preview-185451687 56 11/14/2024
6.2.1 63 11/14/2024
6.2.1-preview-180716003 55 11/14/2024
6.2.0 70 11/14/2024
6.1.1 54 11/14/2024
6.1.1-preview-176300030 61 11/14/2024
6.1.1-preview-175769056 59 12/02/2024
6.1.0 79 09/05/2022
6.1.0-preview-165373954 56 11/14/2024
6.1.0-preview-163077268 53 11/14/2024
6.1.0-preview-162971117 58 11/14/2024
6.0.1 61 11/14/2024
6.0.0 58 11/14/2024
6.0.0-preview-153999281 63 11/14/2024
5.2.0 57 11/14/2024
5.2.0-preview-152861792 57 11/14/2024
5.2.0-preview-150458135 64 11/14/2024
5.1.4 66 11/14/2024
5.1.4-preview-147174683 61 11/14/2024
5.1.3 62 11/14/2024
5.1.3-preview-144669387 68 11/14/2024
5.1.3-preview-144222698 64 11/14/2024
5.1.2 65 12/02/2024
5.1.2-preview-143176668 66 11/27/2024
5.1.1 56 12/07/2024
5.1.1-preview-141622241 58 11/14/2024
5.1.1-preview-140603701 67 11/26/2024
5.1.0 64 11/14/2024
5.1.0-preview-138879827 60 11/14/2024
5.0.3 69 11/14/2024
5.0.2 55 11/14/2024
5.0.2-preview-136262472 49 11/14/2024
5.0.2-preview-136113481 61 11/14/2024
5.0.2-preview-135224594 63 12/02/2024
5.0.2-preview-134636747 57 11/14/2024
5.0.1 54 11/14/2024
5.0.1-preview-133783177 56 11/27/2024
5.0.0 65 11/14/2024
5.0.0-preview-132888327 57 11/14/2024
5.0.0-preview-132124922 63 11/30/2024
5.0.0-preview-131679580 60 06/02/2024
5.0.0-preview-126609691 67 06/02/2024
3.1.2 66 11/14/2024
3.1.2-preview-121943182 60 06/02/2024
3.1.2-preview-120665033 59 06/02/2024
3.1.2-preview-120490779 66 06/02/2024
3.1.2-preview-119453473 57 06/02/2024
3.1.1 71 11/14/2024
3.1.1-preview-115802637 57 06/02/2024
3.1.0 64 11/14/2024
3.1.0-preview-114160390 59 06/02/2024
3.1.0-preview-112969177 58 06/02/2024
3.1.0-preview-112250362 65 06/02/2024
3.1.0-preview-111117527 58 06/02/2024
3.1.0-preview-108719052 65 06/02/2024
3.0.4 60 11/14/2024
3.0.4-preview-106413158 59 06/02/2024
3.0.4-preview-103991021 65 06/02/2024
3.0.4-preview-102596937 62 06/02/2024
3.0.4-preview-102593320 70 06/02/2024
3.0.3 68 11/14/2024
3.0.3-preview-98619331 62 06/02/2024
3.0.2 62 11/14/2024
3.0.2-preview-97503759 68 06/02/2024
3.0.1 60 11/14/2024
3.0.1-preview-95233114 61 06/02/2024
3.0.1-preview-94915444 64 11/14/2024
3.0.0 62 11/14/2024
3.0.0-preview-93361469 69 06/02/2024
3.0.0-preview-92995853 55 06/02/2024
3.0.0-preview-92907246 54 06/02/2024
2.6.0 58 11/14/2024
2.6.0-preview-82454970 62 06/02/2024
2.6.0-preview-82001197 65 06/02/2024
2.6.0-preview-80821564 61 06/02/2024
2.6.0-preview-79432176 66 06/02/2024
2.5.1 63 11/14/2024
2.5.1-preview-75824665 57 06/02/2024
2.5.1-preview-73792921 58 06/02/2024
2.5.1-preview-73031417 61 06/02/2024
2.5.0 64 11/14/2024
2.5.0-preview-69219007 62 06/02/2024
2.5.0-preview-69210974 65 06/02/2024
2.5.0-preview-68640186 60 06/02/2024
2.5.0-preview-67093158 55 06/02/2024
2.4.2 66 11/14/2024
2.4.2-preview-62147279 59 06/02/2024
2.4.1 65 11/27/2024
2.4.0 69 11/14/2024
2.4.0-preview-58415569 74 06/02/2024
2.4.0-preview-58238865 75 06/02/2024
2.3.1 62 11/14/2024
2.3.1-preview-57307518 67 06/02/2024
2.3.1-preview-53660607 67 06/02/2024
2.3.1-preview-53320926 68 06/02/2024
2.3.0 61 11/14/2024
2.2.6-preview-50057154 59 06/02/2024
2.2.6-preview-50053657 73 06/02/2024
2.2.6-preview-49112414 67 06/02/2024
2.2.5 64 11/14/2024
2.2.5-preview-45566217 72 06/02/2024
2.2.5-preview-45139132 63 06/02/2024
2.2.4 66 11/14/2024
2.2.3-preview-43309801 67 11/14/2024
2.2.2 67 11/14/2024
2.2.2-preview-40816597 64 06/02/2024
2.2.1 68 11/14/2024
2.2.0 76 11/14/2024
2.2.0-preview-40294348 62 06/02/2024
2.2.0-preview-38490295 71 06/02/2024
2.2.0-preview-37969809 67 06/02/2024
2.2.0-preview-37943663 61 06/02/2024
2.1.4 64 11/03/2024
2.1.4-preview-34848409 67 06/02/2024
2.1.4-preview-34825232 60 06/02/2024
2.1.4-preview-33704197 67 06/02/2024
2.1.3 67 11/14/2024
2.1.3-preview-33358922 75 06/02/2024
2.1.3-preview-33191223 75 06/02/2024
2.1.3-preview-31198104 63 06/02/2024
2.1.2 66 11/14/2024
2.1.2-preview-30288174 62 06/02/2024
2.1.2-preview-30286136 64 06/02/2024
2.1.2-preview-29226626 59 06/02/2024
2.1.2-preview-28879945 69 06/02/2024
2.1.2-preview-28782089 74 06/02/2024
2.1.1 62 11/14/2024
2.1.1-preview-28628301 71 06/02/2024
2.1.1-preview-28414190 68 06/02/2024
2.1.0 63 11/14/2024
2.1.0-preview-26231671 68 06/02/2024
2.1.0-preview-25885614 57 06/02/2024
2.0.2 66 11/14/2024
2.0.1 65 11/14/2024
2.0.1-preview-21560113 70 06/02/2024
2.0.1-preview-21392243 66 06/02/2024
2.0.0 64 11/14/2024
2.0.0-preview-20091963 59 06/02/2024
2.0.0-preview-19793485 97 06/02/2024
1.2.0-preview-00019208119 65 05/28/2024
1.1.1-preview-00018720262 67 05/28/2024
1.1.0 66 12/03/2024
1.1.0-preview-00018359725 63 05/28/2024
1.1.0-preview-00018287510 65 05/28/2024
1.1.0-preview-00018199934 66 05/28/2024
1.1.0-preview-00017833331 64 05/28/2024
1.1.0-preview-00017748473 65 05/28/2024
1.0.1 69 11/14/2024
1.0.1-preview-00017636063 66 05/28/2024
1.0.1-preview-00017490435 64 05/28/2024
1.0.1-preview-00017285652 63 05/28/2024
1.0.0 62 11/14/2024
0.1.0-ci-00016390477 58 11/14/2024
0.1.0-ci-00016261636 63 11/14/2024
0.1.0-ci-00016020116 57 11/14/2024
0.1.0-ci-00016011622 63 11/14/2024
0.1.0-ci-00015583876 65 11/14/2024