Commit 27b59b8c authored by Savorboard's avatar Savorboard Committed by GitHub

Update README.md

parent e50ec84d
...@@ -60,34 +60,34 @@ First,You need to config CAP in your Startup.cs: ...@@ -60,34 +60,34 @@ First,You need to config CAP in your Startup.cs:
```cs ```cs
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
...... //......
services.AddDbContext<AppDbContext>(); services.AddDbContext<AppDbContext>();
services.AddCap(x => services.AddCap(x =>
{ {
// If you are using EF, you need to add the following configuration: // If you are using EF, you need to add the following configuration:
// Notice: You don't need to config x.UseSqlServer(""") again! CAP can autodiscovery. // Notice: You don't need to config x.UseSqlServer(""") again! CAP can autodiscovery.
x.UseEntityFramework<AppDbContext>(); x.UseEntityFramework<AppDbContext>();
// If you are using ado.net,you need to add the configuration: // If you are using ado.net,you need to add the configuration:
x.UseSqlServer("Your ConnectionStrings"); x.UseSqlServer("Your ConnectionStrings");
x.UseMySql("Your ConnectionStrings"); x.UseMySql("Your ConnectionStrings");
x.UsePostgreSql("Your ConnectionStrings"); x.UsePostgreSql("Your ConnectionStrings");
// If you are using RabbitMQ, you need to add the configuration: // If you are using RabbitMQ, you need to add the configuration:
x.UseRabbitMQ("localhost"); x.UseRabbitMQ("localhost");
// If you are using Kafka, you need to add the configuration: // If you are using Kafka, you need to add the configuration:
x.UseKafka("localhost"); x.UseKafka("localhost");
}); });
} }
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)
{ {
..... //.....
app.UseCap(); app.UseCap();
} }
``` ```
...@@ -99,27 +99,27 @@ Inject `ICapPublisher` in your Controller, then use the `ICapPublisher` to send ...@@ -99,27 +99,27 @@ Inject `ICapPublisher` in your Controller, then use the `ICapPublisher` to send
```c# ```c#
public class PublishController : Controller public class PublishController : Controller
{ {
private readonly AppDbContext _dbContext; private readonly AppDbContext _dbContext;
public PublishController(AppDbContext dbContext) public PublishController(AppDbContext dbContext)
{ {
_dbContext = dbContext; _dbContext = dbContext;
} }
[Route("~/checkAccountWithTrans")] [Route("~/checkAccountWithTrans")]
public async Task<IActionResult> PublishMessageWithTransaction([FromServices]ICapPublisher publisher) public async Task<IActionResult> PublishMessageWithTransaction([FromServices]ICapPublisher publisher)
{ {
using (var trans = dbContext.Database.BeginTransaction()) using (var trans = dbContext.Database.BeginTransaction())
{ {
// your business code // your business code
//Achieving atomicity between original database operation and the publish event log thanks to a local transaction //Achieving atomicity between original database operation and the publish event log thanks to a local transaction
await publisher.PublishAsync("xxx.services.account.check", new Person { Name = "Foo", Age = 11 }); await publisher.PublishAsync("xxx.services.account.check", new Person { Name = "Foo", Age = 11 });
trans.Commit(); trans.Commit();
} }
return Ok(); return Ok();
} }
} }
``` ```
...@@ -133,13 +133,13 @@ Add the Attribute `[CapSubscribe()]` on Action to subscribe message: ...@@ -133,13 +133,13 @@ Add the Attribute `[CapSubscribe()]` on Action to subscribe message:
```c# ```c#
public class PublishController : Controller public class PublishController : Controller
{ {
[CapSubscribe("xxx.services.account.check")] [CapSubscribe("xxx.services.account.check")]
public async Task CheckReceivedMessage(Person person) public async Task CheckReceivedMessage(Person person)
{ {
Console.WriteLine(person.Name); Console.WriteLine(person.Name);
Console.WriteLine(person.Age); Console.WriteLine(person.Age);
return Task.CompletedTask; return Task.CompletedTask;
} }
} }
``` ```
...@@ -148,34 +148,33 @@ public class PublishController : Controller ...@@ -148,34 +148,33 @@ public class PublishController : Controller
If your subscribe method is not in the Controller,then your subscribe class need to Inheritance `ICapSubscribe`: If your subscribe method is not in the Controller,then your subscribe class need to Inheritance `ICapSubscribe`:
```cs ```c#
namespace xxx.Service namespace xxx.Service
{ {
public interface ISubscriberService public interface ISubscriberService
{ {
public void CheckReceivedMessage(Person person); public void CheckReceivedMessage(Person person);
} }
public class SubscriberService: ISubscriberService, ICapSubscribe
{
[CapSubscribe("xxx.services.account.check")]
public void CheckReceivedMessage(Person person)
{
} public class SubscriberService: ISubscriberService, ICapSubscribe
} {
[CapSubscribe("xxx.services.account.check")]
public void CheckReceivedMessage(Person person)
{
}
}
} }
``` ```
Then inject your `ISubscriberService` class in Startup.cs Then inject your `ISubscriberService` class in Startup.cs
```cs ```c#
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddTransient<ISubscriberService,SubscriberService>(); services.AddTransient<ISubscriberService,SubscriberService>();
} }
``` ```
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment