DotNetCore.CAP 7.2.0-preview-207205557

CAP                     中文

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

CAP is a library based on .Net standard, which is a solution to deal with distributed transactions, has the function of EventBus, it is lightweight, easy to use, and efficient.

In the process of building an SOA or MicroService system, we usually need to use the event to integrate each service. In the process, simple use of message queue does not guarantee reliability. CAP adopts local message table program integrated with the current database to solve exceptions that may occur in the process of the distributed system calling each other. It can ensure that the event messages are not lost in any case.

You can also use CAP as an EventBus. CAP provides a simpler way to implement event publishing and subscriptions. You do not need to inherit or implement any interface during subscription and sending process.

Architecture overview

cap.png

CAP implements the Outbox Pattern described in the eShop ebook.

Getting Started

NuGet

CAP can be installed in your project with the following command.

PM> Install-Package DotNetCore.CAP

CAP supports most popular message queue as transport, following packages are available to install:

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

CAP supports most popular database as event storage, following packages are available to install:

// select a database provider you are using, event log table will integrate into.

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

Configuration

First, you need to configure CAP in your Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    //......

    services.AddDbContext<AppDbContext>(); //Options, If you are using EF as the ORM
    services.AddSingleton<IMongoClient>(new MongoClient("")); //Options, If you are using MongoDB

    services.AddCap(x =>
    {
        // If you are using EF, you need to add the configuration:
        x.UseEntityFramework<AppDbContext>(); //Options, Notice: You don't need to config x.UseSqlServer(""") again! CAP can autodiscovery.

        // If you are using ADO.NET, choose to add configuration you needed:
        x.UseSqlServer("Your ConnectionStrings");
        x.UseMySql("Your ConnectionStrings");
        x.UsePostgreSql("Your ConnectionStrings");

        // If you are using MongoDB, you need to add the configuration:
        x.UseMongoDB("Your ConnectionStrings");  //MongoDB 4.0+ cluster

        // CAP support RabbitMQ,Kafka,AzureService as the MQ, choose to add configuration you needed:
        x.UseRabbitMQ("HostName");
        x.UseKafka("ConnectionString");
        x.UseAzureServiceBus("ConnectionString");
        x.UseAmazonSQS();
    });
}

Publish

Inject ICapPublisher in your Controller, then use the ICapPublisher to send messages.

The version 7.0+ supports publish delay 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))
        {
            using (var transaction = connection.BeginTransaction(_capBus, autoCommit: true))
            {
                //your business logic code

                _capBus.Publish("xxx.services.show.time", DateTime.Now);

                // Publish delay message
                _capBus.PublishDelayAsync(TimeSpan.FromSeconds(delaySeconds), "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 code

            _capBus.Publish("xxx.services.show.time", DateTime.Now);
        }

        return Ok();
    }
}

Subscribe

In Controller Action

Add the Attribute [CapSubscribe()] on Action to subscribe to messages:

public class PublishController : Controller
{
    [CapSubscribe("xxx.services.show.time")]
    public void CheckReceivedMessage(DateTime datetime)
    {
        Console.WriteLine(datetime);
    }
}

In Business Logic Service

If your subscription method is not in the Controller, then your subscribe class needs to implement 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)
        {
        }
    }
}

Then register your class that implements ISubscriberService in Startup.cs

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

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

Async subscription

You are able to implement async subscription. Subscription's method should return Task and receive CancellationToken as parameter.

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

Use partials for topic subscriptions

To group topic subscriptions on class level you're able to define a subscription on a method as a partial. Subscriptions on the message queue will then be a combination of the topic defined on the class and the topic defined on the method. In the following example the Create(..) function will be invoked when receiving a message on customers.create

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

Subscribe Group

The concept of a subscription group is similar to that of a consumer group in Kafka. it is the same as the broadcast mode in the message queue, which is used to process the same message between multiple different microservice instances.

When CAP startups, it will use the current assembly name as the default group name, if multiple same group subscribers subscribe to the same topic name, there is only one subscriber that can receive the message. Conversely, if subscribers are in different groups, they will all receive messages.

In the same application, you can specify Group property to keep subscriptions in different subscribe groups:


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

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

ShowTime1 and ShowTime2 will be called one after another because all received messages are processed linear. You can change that behaviour to set UseDispatchingPerGroup true.

BTW, You can specify the default group name in the configuration:

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

Dashboard

CAP also provides dashboard pages, you can easily view messages that were sent and received. In addition, you can also view the message status in real time in the dashboard. Use the following command to install the Dashboard in your project.

PM> Install-Package DotNetCore.CAP.Dashboard

In the distributed environment, the dashboard built-in integrates Consul as a node discovery, while the realization of the gateway agent function, you can also easily view the node or other node data, It's like you are visiting local resources.

View Consul config docs

If your service is deployed in Kubernetes, please use our Kubernetes discovery package.

PM> Install-Package DotNetCore.CAP.Dashboard.K8s

View Kubernetes config docs

The dashboard default address is: http://localhost:xxx/cap , you can configure relative path /cap with x.UseDashboard(opt =>{ opt.MatchPath="/mycap"; }).

Contribute

One of the easiest ways to contribute is to participate in discussions and discuss issues. You can also contribute by submitting pull requests with code changes.

License

MIT

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

Packages Downloads
ZhonTai.Admin
中台Admin权限管理接口库
90
ZhonTai.Admin
中台Admin权限管理接口库
91
ZhonTai.Admin
中台Admin权限管理接口库
92
ZhonTai.Admin
中台Admin权限管理接口库
93
ZhonTai.Admin
中台Admin权限管理接口库
95
ZhonTai.Admin
中台Admin权限管理接口库
96
ZhonTai.Admin
中台Admin权限管理接口库
98
ZhonTai.Admin
中台Admin权限管理接口库
99
ZhonTai.Admin
中台Admin权限管理接口库
100
ZhonTai.Admin
中台Admin权限管理接口库
105
ZhonTai.Admin
中台Admin权限管理接口库
107
ZhonTai.Admin
中台Admin权限管理接口库
111
ZhonTai.Admin
中台Admin权限管理接口库
112
ZhonTai.Admin
中台Admin权限管理接口库
118
ZhonTai.Admin
中台Admin权限管理接口库
119
ZhonTai.Admin.Core
中台Admin权限管理核心库
90
ZhonTai.Admin.Core
中台Admin权限管理核心库
91
ZhonTai.Admin.Core
中台Admin权限管理核心库
93

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