netcore-cap-using
CAP 是一个基于 .NET Standard 的 C# 库,它是一种处理分布式事务的解决方案,同样具有 EventBus 的功能
使用非常简单
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
x.UseDashboard();
// CAP support RabbitMQ,Kafka,AzureService as the MQ, choose to add configuration you needed:
x.UseRabbitMQ("localhost");
// x.UseKafka("ConnectionString");
// x.UseAzureServiceBus("ConnectionString");
});
app.UseCap();
public class PublishController : Controller
{
private readonly ICapPublisher _capBus;
public PublishController(ICapPublisher capPublisher)
{
_capBus = capPublisher;
}
//不使用事务
[Route("~/without/transaction")]
public IActionResult WithoutTransaction()
{
_capBus.Publish("xxx.services.show.time", DateTime.Now);
return Ok();
}
[CapSubscribe("xxx.services.show.time")]
public async Task CheckReceivedMessage(DateTime time)
{
Console.WriteLine(time);
await Task.CompletedTask;
}
}